Sorting repeat with list

I need to sort a repeat with list. I have a list of files which I guess are sorted but not the way I need. Specifically, I need them sorted without regard to length. For example given the filenames that are the numbers 1-25 the nomal sort order seems to be:

1.jpg
10.jg
11.jpg
.
.
.
19.jpg
2.jpg
20.jpg
.
.
.
25.jpg
3.jpg
.
.
.
9.jpg

What I need is:

1.jpg
2.jpg
.
.
.
25.jpg

Is this possible and if so how? TIA.

hello

first idea:

build a parallel list with names modified this way

1.jpg → 001.jpg
12.jpg → 012.jpg

Then sort the associated lists using the new one as the key one.

second idea:
maybe there is such a tool in the unix pandora box.

Yvan KOENIG (from FRANCE lundi 9 octobre 2006 21:38:13)

Like so, perhaps (provided that all the extension are “.jpg”):


property numList : {}
property NameList : {}

set txtList to "1.jpg
10.jpg
11.jpg
19.jpg
2.jpg
20.jpg
25.jpg
3.jpg
9.jpg"

set NameList to paragraphs of txtList

repeat with F in NameList
	set end of numList to (text 1 thru ((offset of ".jpg" in (contents of F)) - 1) of F) as string as number
end repeat

sort_items(numList, NameList)
{numList, NameList}

to sort_items(sortList, SecondList) -- from a script by Kai
	tell (count sortList) to repeat with i from (it - 1) to 1 by -1
		set s to sortList's item i
		set r to SecondList's item i
		repeat with i from (i + 1) to it
			tell sortList's item i to if s > it then
				set sortList's item (i - 1) to it
				set SecondList's item (i - 1) to SecondList's item i
			else
				set sortList's item (i - 1) to s
				set SecondList's item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > sortList's end then
			set sortList's item it to s
			set SecondList's item it to r
		end if
	end repeat
end sort_items
-->  {{1, 2, 3, 9, 10, 11, 19, 20, 25}, {"1.jpg", "2.jpg", "3.jpg", "9.jpg", "10.jpg", "11.jpg", "19.jpg", "20.jpg", "25.jpg"}}

If you wanted it back as a text list:


property numList : {}
property NameList : {}

set txtList to "1.jpg -- this list is not kept; it's replaced.
10.jpg
11.jpg
19.jpg
2.jpg
20.jpg
25.jpg
3.jpg
9.jpg"

set NameList to paragraphs of txtList

repeat with F in NameList
	set end of numList to (text 1 thru ((offset of ".jpg" in (contents of F)) - 1) of F) as string as number
end repeat

sort_items(numList, NameList)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set txtList to NameList as string
set AppleScript's text item delimiters to tid
txtList

to sort_items(sortList, SecondList) -- from a script by Kai
	tell (count sortList) to repeat with i from (it - 1) to 1 by -1
		set s to sortList's item i
		set r to SecondList's item i
		repeat with i from (i + 1) to it
			tell sortList's item i to if s > it then
				set sortList's item (i - 1) to it
				set SecondList's item (i - 1) to SecondList's item i
			else
				set sortList's item (i - 1) to s
				set SecondList's item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > sortList's end then
			set sortList's item it to s
			set SecondList's item it to r
		end if
	end repeat
end sort_items

Hi.

It’s not clear from the original query whether the aim is to sort the names or the files. Just to add to the “name sort” contributions, if all the names are low-order numerics as in the examples, this eccentricity’s quite fast:

set my_list to {"2.jpg", "19.jpg", "11.jpg", "3.jpg", "25.jpg", "9.jpg", "1.jpg", "20.jpg", "18.jpg", "4.jpg", "22.jpg", "5.jpg", "23.jpg", "6.jpg", "24.jpg", "7.jpg", "26.jpg", "8.jpg", "10.jpg", "17.jpg", "16.jpg", "15.jpg", "14.jpg"}
sortNumericFileNames(my_list)

on sortNumericFileNames(nameList)
	script o
		property list1 : nameList
		property list2 : {}
	end script
	
	set len1 to (count nameList)
	set len2 to len1
	repeat with i from 1 to len1
		set end of o's list2 to missing value
		set n to (word 1 of item i of o's list1) as integer
		if (n > len2) then set len2 to n
	end repeat
	
	repeat len2 - len1 times
		set end of o's list2 to missing value
	end repeat
	
	repeat with i from 1 to len1
		set this_name to item i of o's list1
		set item (word 1 of this_name) of o's list2 to this_name as Unicode text
	end repeat
	
	return o's list2's every Unicode text
end sortNumericFileNames

To sort a list of the files themselves, the Finder’s sort command might be what you need. I believe it sorts numeric strings numerically by default when sorting by name, though I can’t check that at the moment. It returns a list of Finder references though.

tell application "Finder"
	set sorted_files to (sort my_files by name)
end tell

Thanks for all the suggestions. Quite a bit to digest all at once. I didn’t think it would be this complicated and may not be worth while in this particular case. I’m going to have to take some time to try and understand all this as I am still an Applescript beginner. If I don’t use it now, I may well need it later.

If you’re on Tiger, wrap your sort in a ‘considering numeric strings’ block:

on _sort(k, a, b)
	if a ≥ b then return
	set p to item b of k's lst
	set l to a
	set r to b - 1
	repeat while l ≤ r
		repeat while l ≤ r and item l of k's lst ≤ p
			set l to l + 1
		end repeat
		repeat while l ≤ r and item r of k's lst ≥ p
			set r to r - 1
		end repeat
		if l < r then
			set {item l of k's lst, item r of k's lst} to {item r of k's lst, item l of k's lst}
		end if
	end repeat
	set {item l of k's lst, item b of k's lst} to {item b of k's lst, item l of k's lst}
	_sort(k, a, l - 1)
	_sort(k, l + 1, b)
end _sort

on sortList(theList)
	script k
		property lst : every item of theList
	end script
	_sort(k, 1, length of k's lst)
	return k's lst
end sortList

set theList to {"2.jpg", "19.jg", "11.jpg", "3.jpg", "25.jpg", "9.jpg", "1.jpg", "20.jpg", "18.jpg", "4.jpg", "22.jpg", "5.jpg", "23.jpg", "6.jpg", "24.jpg", "7.jpg", "26.jpg", "8.jpg", "10.jpg", "17.jpg", "16.jpg", "15.jpg", "14.jpg"}
considering numeric strings
	set sortedList to sortList(theList)
end considering
--> {"1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "14.jpg", "15.jpg", "16.jpg", "17.jpg", "18.jpg", "19.jg", "20.jpg", "22.jpg", "23.jpg", "24.jpg", "25.jpg", "26.jpg"}

HTH