A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Sort list of file names logically

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Posts
    10

    Sort list of file names logically

    Hi there,

    I am near the point of breakdown trying to figure this out it's beyond my capabilities and maybe even brain power. I have a list of file names and I want to sort them as any logical human would.

    728x250_expandable_frame1.jpg
    to
    728x250_expandable_frame15.jpg

    At the moment when I use:-
    Code:
    files.sort(Array.CASEINSENSITIVE);
    It assumes that frame4.jpg is greater than frame15.jpg so the order comes out:-
    frame1, frame11, frame12, frame13

    when obviously i want frame1, frame2, frame3

    Any help would be greatly appreciated.

    Will

    P.s. I realised i could just name the files 01, 02, 03 but really I don't want to have to do this as it supposed to be an automated system.

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    The reason you are running into the problem is because it isn't interpreting the number as numbers but as a string. You may need to add a method to separate the file name out and manually (through code) sort of file names, which will account for the string prefix. You may need to analyze by parsing out the number and then evaluating as a number instead of a string. Using the included sort function would be nice and can be partially used, but you may be force to build your own method that sort to your specifications.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Try this:

    PHP Code:
    var files = new Array("728x250_expandable_frame1.jpg""728x250_expandable_frame2.jpg""728x250_expandable_frame3.jpg""728x250_expandable_frame4.jpg""728x250_expandable_frame5.jpg""728x250_expandable_frame6.jpg""728x250_expandable_frame7.jpg""728x250_expandable_frame8.jpg""728x250_expandable_frame9.jpg""728x250_expandable_frame10.jpg""728x250_expandable_frame11.jpg""728x250_expandable_frame12.jpg");
    var 
    myarr = new Array();
    for (var 
    0i<files.lengthi++) {
        var 
    getstr files[i].indexOf("_frame")+6;
        var 
    str1 files[i].substr(0getstr);
        var 
    str2 files[i].substr(getstr);
        var 
    strar str2.split(".");
        var 
    obj = new Object();
        
    obj.firstpart str1;
        
    obj.thenumber Number(strar[0]);
        
    obj.filetype "."+strar[1];
        
    myarr.push(obj);
    }
    myarr.sortOn("thenumber",Array.NUMERIC);
    for (var 
    0j<myarr.lengthj++) {
        
    files[j] = myarr[j].firstpart+myarr[j].thenumber+myarr[j].filetype;
    }
    trace(files); 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center