I noticed one oddity with how
comonFunctions.ListFiles("FolderPath"); works.
It kind of sorts the filenames wrong. For example, if you have files "brunette99.jpg" and "brunette100.jpg", then for some reason it puts the latter to the front, as if the filename was smaller, even though it isn't. [spoiler = "the rest of original question"]So when I get the filenames for all the images in the folder with the aim to display them in sequence, I sometimes get the unwanted result of pictures from the end of the set being shown first.
Any ideas how to fix this? If file names were uniform, I would run a simple sorting script, but I can't. I still would like to get the files in the order Windows sorts them.
Basically, if you have files named "lalalaX.jpg" where X ranges from 1 to 100, you will get the files in sequence of:
lalala1
lalala10
lalala100
lalala11
lalala12
lalala13
.....
lalala19
lalala2
lalala20
which is not really great =\[/spoiler]
OK, I found a solution. Here's how to implement a smart string comparison in javascript (the function). After that you can run any kind of sorting you want.
Code: Select all
function fSmartStringCmp(x,y)
{
if ((x == null) && (y == null))
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
var lx, ly;
lx = x.length;
ly = y.length;
for (var mx = 0, my = 0; ((mx < lx) && (my < ly)); mx++, my++)
{
if ((!isNaN(parseInt(x[mx]))) && (!isNaN(parseInt(y[my]))))
{
//both characters are numbers
var vx = 0, vy = 0;
for (; (mx < lx) && (!isNaN(parseInt(x[mx]))); mx++)
{
vx = vx * 10 + parseInt(x[mx]);
}
for (; (my < ly) && (!isNaN(parseInt(y[my]))); my++)
{
vy = vy * 10 + parseInt(y[my]);
}
if (vx != vy)
{
return vx > vy ? 1 : -1;
}
}
if ((mx < lx) && (my < ly) && (x[mx] != y[my]))
{
return x[mx] > y[my] ? 1 : -1;
}
}
if ((lx - ly)>0)
{
return 1;
}
else if
{
return -1;
}
}
This will return "-1" if X is a smaller name than Y, "0" if they are equal, and "1" if Y is bigger.
And to give credits:
http://stackoverflow.com/questions/1601 ... ll#1601834