Copy link to clipboard
Copied
Hi,
I am getting the list of files in a specified folder using getFiles('*.jpg') method.
My problem is how getFiles() sorting the list?
I tried to refer API document for getFiles() but did not found a sorting description.
Fellows:
Initial topic was about default sorting. I thought it is due to Ascii character based sorting but seems it is also not. Kindly see default list *.jpg sorting.
File Name | Ascii Value | Order Preserve? |
---|---|---|
1.jpg | 49 | Yes |
2.jpg | 50 | Yes |
a1.jpg | 97, 49 | Yes |
a1@0.jpg | 97, 49, 64, 48 | Yes |
a1A0.jpg | 97, 49, 65, 48 | Yes |
`.jpg | 96 | No |
~a1.jpg | 126, 97, 49 | No |
For the above jpg list the order is almost followed ASC sorting by Ascii value but incorrect for
...Copy link to clipboard
Copied
You need to sort them like this:
var folder = new Folder("~/Desktop/sorting");
var files = folder.getFiles("*.jpg");
files.sort(); // ascending
files.reverse(); // if need to be descending, use this
for (var i = 0; i < files.length; i++) $.writeln(decodeURI(File(files).name));
You can use other functions to personalize other sorting advanced methods also. Depends on what you are dealing with.
Copy link to clipboard
Copied
I never heard of .reverse() method
Copy link to clipboard
Copied
You can use other functions to personalize other sorting advanced methods also. Depends on what you are dealing with.
Thanks Pedro. Your explanation is fine to understand 'How to sort'.
But my original question was not intended to understand how the returned file list is sorted by default?
I am not checking the way of 'How to sort it'.
In initial research, I found that that list is sorted as ASC order.
-> a.jpg, c.jpg and b.jpg will be listed and returned as a.jpg, b.jpg, c.jpg order.
Is it sorted by Name?
If yes, then how about the complex scenarios like a.jpg, _a.jpg, #_1a.jpg, A.jpg?
Is this according to windows file name sorting?
If someone could explain this using the correct API reference of getFiles() would be most helpful.
Copy link to clipboard
Copied
No, it's not the same like in Windows. It's by Ascii numbers bound to each character I think.
For ex. you'll get 1, 10, 2 order. To change it you have to write own function:
folder.getFiles(function(){return /**/})
Copy link to clipboard
Copied
>> No, it's not the same like in Windows. It's by Ascii numbers bound to each character I think.
Thank you very much for your explanation Kukurykus.
Can you suggest any reference or API document to understand this correctly?
[I can sort but I need to understand a bug of an already written software by JSX]
Copy link to clipboard
Copied
arr = ['1', '2', '10'] // 1, 10, 2
arr.sort(function(v1, v2){return v1 > v2}) // 1, 2, 10
For following example you have to write more complex function:
arr = ['a1', 'a2', 'a10']
Copy link to clipboard
Copied
We added app.compareWithNumbers for Photoshop to get the same results Photoshop would give you.
Copy link to clipboard
Copied
Is it somewhere documented and how to use it?
Copy link to clipboard
Copied
Hi Tom
app.compareWithNumbers
Can you explain it?
Why isn't documented?
Copy link to clipboard
Copied
There are a couple of functions in ContactSheetII
//
// Function: caseInsensitiveFileSort
// Description: sorts an array of files case insensitive
// Input: list - an array of files
// Return: a sorted array of files
//
ContactSheetII.caseInsensitiveFileSort = function(list) {
function ciCmp(a, b) {
return app.compareWithNumbers (a.name, b.name)
}
return list.sort(ciCmp);
};
//
// Function: caseInsensitiveSort
// Description: sorts an array of strings case insensitive
// Input: list - an array of strings
// Return: a sorted array of strings
//
ContactSheetII.caseInsensitiveSort = function(list) {
function ciCmp(a, b) {
return app.compareWithNumbers (a, b)
}
return list.sort(ciCmp);
};
Copy link to clipboard
Copied
['a1a2', 'a1a10', 'a1a1'].sort(function(a, b){return compareWithNumbers(a, b)})
So it sorts now the same way like in Windows with compareWithNumbers!
For ex. I put to Files folder a1a2.txt, a1a10.txt and a1a1.txt:
File('~/Desktop/Files').getFiles().sort(function(a, b){return compareWithNumbers(a.name, b.name)})
btw instead of a.name (and b.name) you can use also String(a), uneval(a), decodeURI(a), so for full path.
Copy link to clipboard
Copied
https://www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop-cc-javascript-ref-2019.pdf
Not available in all versions of Photoshop. I would use it in CC 2019 ONLY as we had an issue in previous versions.
Copy link to clipboard
Copied
Is that example in description with A1, A2, A10 based on my post? It would be unusual coincidence I thought up something that already existed Did you update it now or that information was already avialable from some time?
Till now I had to use complex function (that I spent quite long time to write) to sort files like Windows does, so I wonder if you could share with us the function that's hidden 'beneath' app.compareWithNumbers (if you have access to)...
Copy link to clipboard
Copied
Fellows:
Initial topic was about default sorting. I thought it is du to Ascii character based sorting but seems it is also not.
File Name | Ascii Value | Sorting order correct? |
---|---|---|
1.jpg | 1 | Yes |
2.jpg | 2 | Yes |
a1.jpg | 97, 1 | Yes |
a1@0.jpg | 97, 1, 64, 0 | Yes |
a1@0.jpg | 97, 1, 65, 0 | Yes |
`.jpg | 96 | NO |
~a1.jpg | 126, 97, 1 | NO |
For above jpg list the order is almost follows ASC sorting by Ascii value but incorrect for `.jpg and ~a1.jpg.
Copy link to clipboard
Copied
Fellows:
Initial topic was about default sorting. I thought it is due to Ascii character based sorting but seems it is also not. Kindly see default list *.jpg sorting.
File Name | Ascii Value | Order Preserve? |
---|---|---|
1.jpg | 49 | Yes |
2.jpg | 50 | Yes |
a1.jpg | 97, 49 | Yes |
a1@0.jpg | 97, 49, 64, 48 | Yes |
a1A0.jpg | 97, 49, 65, 48 | Yes |
`.jpg | 96 | No |
~a1.jpg | 126, 97, 49 | No |
For the above jpg list the order is almost followed ASC sorting by Ascii value but incorrect for `.jpg and ~a1.jpg. I think it would be better to conclude that folder.getFiles() is not sorted at all by default.
Copy link to clipboard
Copied
You may delete your last topic and leave (updated) the one before last
btw you have incorrect ASCII values. Where you found 1 is 1, not 49?
...and only ` should be in your column on 3rd position:
1: 49
2: 50
a1: 97 + 49
a1@0: 97 + 49 + 64 + 48
a1@0: 97 + 49 + 65 + 48
`: 96
~a`: 126 + 97 + 49
Copy link to clipboard
Copied
Kukurykus wrote
You may delete your last topic and leave (updated) the one before last
Of course we should do..but unfortunately, it is not allowing me to perform delete action. -> says "No actions are available"
Kukurykus wrote
1: 49
2: 50a1: 97 + 49
a1@0: 97 + 49 + 64 + 48
a1@0: 97 + 49 + 65 + 48
`: 96
~a`: 126 + 97 + 49
Thanks for pointing it was mistakenly referred the hx column value.
Updated the comment.
Copy link to clipboard
Copied
My mistake. I answered to the one before last instead of last one. If I replied to last one you could delete previous
Ps. I assume even Adobe can't remember who and why invented this specific and confusing sorting for getFiles()