At the beginning my script searched numbers in the range 1000 . 1150 as it was described by the asker.

Later it was said that the upper limit may be different so I edited the script to determine the larger number used.

I assumed that the upper limit was supposed to be a multiple of 10.

It’s why I introduced the instructions :

set higherDiv10 to higher div 10

if higherDiv10 * 10 < higher then set higher to (higherDiv10 * 10) + 10

which may be replaced by the more efficient :

set higher to (round (higher / 10) rounding up) * 10

This said, back to your comments.

WIth these files: {“tb0020_comp_v028.1000.dpx”, “tb0020_comp_v028.1002.dpx”, “tb0020_comp_v028.1004.dpx”, “tb0020_comp_v028.999.dpx”}, your script returns no result.

The problem appears to relate to your sort method.

The problem is not the sort method, it’s that your list contains a name which doesn’t match the rule so that your incorrect name “tb0020_comp_v028.999.dpx” is returned at the very end of the sorted list.

With a correctly formed name - “tb0020_comp_v028.0999.dpx”

the sort return 1004 as the greater number available.

My complementary instructions replace this value by 1010.

When it exists, higher is 1; since it’s actually lower, the repeat loop can’t begin.

I don’t understand what allow you to state that if your wrong filename is removed my code returns 1 as higher number. It’s just a nightmare of you.

```
set theNames to {"tb0020_comp_v028.1000.dpx", "tb0020_comp_v028.1002.dpx", "tb0020_comp_v028.1004.dpx", "tb0020_comp_v028.999.dpx"}
# Sort the list of names
set theNames to clasSort(theNames, cmpasc)
# extracts the greater number
set higher to (text -8 thru -5 of last item of theNames) as integer
log result (*999*)
set higherDiv10 to higher div 10
if higherDiv10 * 10 < higher then set higher to (higherDiv10 * 10) + 10
log result (*1000*)
set theNames to {"tb0020_comp_v028.1000.dpx", "tb0020_comp_v028.1002.dpx", "tb0020_comp_v028.1004.dpx", "tb0020_comp_v028.0999.dpx"}
# Sort the list of names
set theNames to clasSort(theNames, cmpasc)
# extracts the greater number
set higher to (text -8 thru -5 of last item of theNames) as integer
log result (*1004*)
set higherDiv10 to higher div 10
if higherDiv10 * 10 < higher then set higher to (higherDiv10 * 10) + 10
log result (*1010*)
set theNames to {"tb0020_comp_v028.1000.dpx", "tb0020_comp_v028.1002.dpx", "tb0020_comp_v028.1004.dpx"}
# Sort the list of names
set theNames to clasSort(theNames, cmpasc)
# extracts the greater number
set higher to (text -8 thru -5 of last item of theNames) as integer
log result (*1004*)
set higher to (round (higher / 10) rounding up) * 10
log result (*1010*)
#=====
(*
Tri par classement
-----------------
ImplÃ©mentation: L. Sebilleau & D. Varlet
*)
on clasSort(lista, fcmp)
if (count of lista) < 2 then return lista
script listb
property liste : lista
property Compare : fcmp
end script
repeat with i from 2 to count of listb's liste
set cle to item i of listb's liste
repeat with j from i - 1 to 1 by -1
if listb's Compare(cle, item j of listb's liste) then
set item (j + 1) of listb's liste to item j of listb's liste
else
set j to j + 1
exit repeat
end if
end repeat
set item j of listb's liste to cle
end repeat
return listb's liste
end clasSort
----------- les fonctions de comparaison ------------
on cmpasc(n1, n2) -- pour le tri ascendant de nombres ou de chaÃ®nes -- USED HERE
return n1 < n2
end cmpasc
on cmpdesc(n1, n2) -- tri descendant de nombres ou de chaÃ®nes
return n1 > n2
end cmpdesc
on cmpBothAsc(n1, n2) -- pour le tri ascendant de listes de nombres et de chaÃ®nes
return n1 as text < n2 as text
end cmpBothAsc
on cmpBothDesc(n1, n2) -- tri descendant de de listes de nombres et de chaÃ®nes
return n1 as text > n2 as text
end cmpBothDesc
on cmpNumAsc(n1, n2) -- pour le tri ascendant des nombres et des chaÃ®nes
considering numeric strings
n1 < n2
end considering
return result
end cmpNumAsc
on cmpNumDesc(n1, n2) -- tri descendant des nombres et des chaÃ®nes
considering numeric strings
n1 > n2
end considering
return result
end cmpNumDesc
on cmpLengthAsc(n1, n2) -- pour le tri ascendant des nombres et des chaÃ®nes
return (count (n1 as text)) < (count (n2 as text))
end cmpLengthAsc
on cmpLengthDesc(n1, n2) -- tri descendant des nombres et des chaÃ®nes
return (count (n1 as text)) > (count (n2 as text))
end cmpLengthDesc
# Sort lists of strings abc â‡¥ def â‡¥ ghi upon first substring before tab
on cmpLengthFirstDesc(t1, t2)
set {oTIDs, text item delimiters} to {text item delimiters, tab}
set tt1 to text item 1 of t1
set tt2 to text item 1 of t2
set text item delimiters to oTIDs
return (count tt1) > (count tt2)
end cmpLengthFirstDesc
# Sort list of lists of several items upon length of the first one
on cmpLengthFirstItemInList(L1, L2)
return (count L1's item 1) > (count L2's item 1)
end cmpLengthFirstItemInList
#=====
```

Of course, if I was a sooth sayer, I would have guess that you used a number with less than 4 digits and would have use the comparator cmpNumAsc.

```
set theNames to {"tb0020_comp_v028.1000.dpx", "tb0020_comp_v028.1002.dpx", "tb0020_comp_v028.1004.dpx", "tb0020_comp_v028.999.dpx"}
# Sort the list of names
set theNames to clasSort(theNames, cmpNumAsc)
# extracts the greater number
set higher to (text -8 thru -5 of last item of theNames) as integer
log result (*1004*)
set higherDiv10 to higher div 10
if higherDiv10 * 10 < higher then set higher to (higherDiv10 * 10) + 10
log result (*1010*)
set theNames to {"tb0020_comp_v028.1000.dpx", "tb0020_comp_v028.1002.dpx", "tb0020_comp_v028.1004.dpx", "tb0020_comp_v028.0999.dpx"}
# Sort the list of names
set theNames to clasSort(theNames, cmpNumAsc)
# extracts the greater number
set higher to (text -8 thru -5 of last item of theNames) as integer
log result (*1004*)
set higherDiv10 to higher div 10
if higherDiv10 * 10 < higher then set higher to (higherDiv10 * 10) + 10
log result (*1010*)
set theNames to {"tb0020_comp_v028.1000.dpx", "tb0020_comp_v028.1002.dpx", "tb0020_comp_v028.1004.dpx"}
# Sort the list of names
set theNames to clasSort(theNames, cmpNumAsc)
# extracts the greater number
set higher to (text -8 thru -5 of last item of theNames) as integer
log result (*1004*)
set higher to (round (higher / 10) rounding up) * 10
log result (*1010*)
#=====
(*
Tri par classement
-----------------
ImplÃ©mentation: L. Sebilleau & D. Varlet
*)
on clasSort(lista, fcmp)
if (count of lista) < 2 then return lista
script listb
property liste : lista
property Compare : fcmp
end script
repeat with i from 2 to count of listb's liste
set cle to item i of listb's liste
repeat with j from i - 1 to 1 by -1
if listb's Compare(cle, item j of listb's liste) then
set item (j + 1) of listb's liste to item j of listb's liste
else
set j to j + 1
exit repeat
end if
end repeat
set item j of listb's liste to cle
end repeat
return listb's liste
end clasSort
----------- les fonctions de comparaison ------------
on cmpasc(n1, n2) -- pour le tri ascendant de nombres ou de chaÃ®nes
return n1 < n2
end cmpasc
on cmpdesc(n1, n2) -- tri descendant de nombres ou de chaÃ®nes
return n1 > n2
end cmpdesc
on cmpBothAsc(n1, n2) -- pour le tri ascendant de listes de nombres et de chaÃ®nes
return n1 as text < n2 as text
end cmpBothAsc
on cmpBothDesc(n1, n2) -- tri descendant de de listes de nombres et de chaÃ®nes
return n1 as text > n2 as text
end cmpBothDesc
on cmpNumAsc(n1, n2) -- pour le tri ascendant des nombres et des chaÃ®nes -- USED HERE
considering numeric strings
n1 < n2
end considering
return result
end cmpNumAsc
on cmpNumDesc(n1, n2) -- tri descendant des nombres et des chaÃ®nes
considering numeric strings
n1 > n2
end considering
return result
end cmpNumDesc
on cmpLengthAsc(n1, n2) -- pour le tri ascendant des nombres et des chaÃ®nes
return (count (n1 as text)) < (count (n2 as text))
end cmpLengthAsc
on cmpLengthDesc(n1, n2) -- tri descendant des nombres et des chaÃ®nes
return (count (n1 as text)) > (count (n2 as text))
end cmpLengthDesc
# Sort lists of strings abc â‡¥ def â‡¥ ghi upon first substring before tab
on cmpLengthFirstDesc(t1, t2)
set {oTIDs, text item delimiters} to {text item delimiters, tab}
set tt1 to text item 1 of t1
set tt2 to text item 1 of t2
set text item delimiters to oTIDs
return (count tt1) > (count tt2)
end cmpLengthFirstDesc
# Sort list of lists of several items upon length of the first one
on cmpLengthFirstItemInList(L1, L2)
return (count L1's item 1) > (count L2's item 1)
end cmpLengthFirstItemInList
#=====
```

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 3 mars 2016 11:25:33