|
-
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.
-
Flash/Flex Developer
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.
-
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 i = 0; i<files.length; i++) { var getstr = files[i].indexOf("_frame")+6; var str1 = files[i].substr(0, getstr); 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 j = 0; j<myarr.length; j++) { 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|