First time here? Check out the FAQ!
x

Sort filename array by suffix2 (1 2 3 ... 10 11. not 1 10 11 2 3)

0 votes
264 views
I've got a concatenation of audio files working but I see that my order is wrong because the audio files are numbered with a single suffix so my array gets ordered before I write my audio files.  text_1.aif text_2.aif .... text_10.aif text_11.aif ...   So my order places files 10 and 11 ahead of 2.  I want order of my array to be 1 2 3 4 .... 10 11 12

Using the Capytalk below on a folder of files I can see my ordering is mucked. How do I implement something similar to suffix2 or sort array by this method?

| filesInSameFolder filenameArray |
filesInSameFolder := 'text_1.aif' sampleFileNamesInSameFolder.

"Collect the filenames into an array."
filenameArray := filesInSameFolder asArray.

filenameArray.

Thanks in advance.
asked Jan 16, 2020 in Capytalk & Smalltalk by jonbellona (Adept) (1,300 points)

1 Answer

0 votes

Here's a way to sort Strings based on the number that follows the first underscore in each String:

| sortedNames names |

names := #( 'text_10' 'text_1' 'text_100').
sortedNames := names asSortedCollection sortBlock: [ :str1 :str2 | (str1 copyFrom: (str1 indexOf: $_) + 1 to: str1 size) asNumber < (str2 copyFrom: (str2 indexOf: $_) + 1 to: str2 size) asNumber ].

sortedNames asArray

answered Jan 17, 2020 by ssc (Savant) (126,620 points)
...