• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Folder.getFiles('*.jpg') Sorted ?

Explorer ,
May 22, 2019 May 22, 2019

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?

  • ASC
  • DSC
  • or it is not sorted at all. Just based on the file arrangement in the windows directory.

I tried to refer API document for getFiles() but did not found a sorting description.

TOPICS
Actions and scripting

Views

4.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 30, 2019 May 30, 2019

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 NameAscii ValueOrder Preserve?
1.jpg49Yes
2.jpg50Yes
a1.jpg97, 49Yes
a1@0.jpg97, 49, 64, 48Yes
a1A0.jpg97, 49, 65, 48Yes
`.jpg96No
~a1.jpg126, 97, 49No

For the above jpg list the order is almost followed ASC sorting by Ascii value but incorrect for

...

Votes

Translate

Translate
Adobe
Enthusiast ,
May 23, 2019 May 23, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 23, 2019 May 23, 2019

Copy link to clipboard

Copied

I never heard of .reverse() method

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 23, 2019 May 23, 2019

Copy link to clipboard

Copied

https://forums.adobe.com/people/Pedro+Cortez+Marques  wrote

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 23, 2019 May 23, 2019

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 /**/})

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2019 May 24, 2019

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]  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 24, 2019 May 24, 2019

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']

http://www.asciitable.com/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
May 28, 2019 May 28, 2019

Copy link to clipboard

Copied

We added app.compareWithNumbers for Photoshop to get the same results Photoshop would give you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2019 May 28, 2019

Copy link to clipboard

Copied

Is it somewhere documented and how to use it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 29, 2019 May 29, 2019

Copy link to clipboard

Copied

Hi Tom

app.compareWithNumbers

Can you explain it?

Why isn't documented?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 29, 2019 May 29, 2019

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);

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 29, 2019 May 29, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
May 29, 2019 May 29, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 29, 2019 May 29, 2019

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)...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 30, 2019 May 30, 2019

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 NameAscii ValueSorting order correct?
1.jpg1Yes
2.jpg2Yes
a1.jpg97, 1Yes
a1@0.jpg97, 1, 64, 0Yes
a1@0.jpg97, 1, 65, 0Yes
`.jpg96NO
~a1.jpg126, 97, 1NO

For above jpg list the order is almost follows ASC sorting by Ascii value but incorrect for `.jpg and ~a1.jpg.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 30, 2019 May 30, 2019

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 NameAscii ValueOrder Preserve?
1.jpg49Yes
2.jpg50Yes
a1.jpg97, 49Yes
a1@0.jpg97, 49, 64, 48Yes
a1A0.jpg97, 49, 65, 48Yes
`.jpg96No
~a1.jpg126, 97, 49No

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2019 May 31, 2019

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 31, 2019 May 31, 2019

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: 50

a1: 97 + 49

a1@0: 97 + 49 + 64 + 48

a1@0: 97 + 49 + 65 + 48

`: 96

~a`: 126 + 97 + 49

http://www.asciitable.com/​

Thanks for pointing it was mistakenly referred the hx column value. 

Updated the comment.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

LATEST

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()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines