Page 1 of 1

array sorting

Posted: Fri Nov 10, 2017 12:30 pm
by sashapont
I have table myImages$={"1.png","2.png","5.png","10.png","5.png","6.png","7.png","8.png","9.png","10.png"}

I try to sort it by names
Sort(myImages$)
For k = 0 To 9
NPrint(myImages$[k])
Next

And have
1,10,2,3,4,5,6,7,8,9

How I can change sorting to have
1,2,3,4,5,6,7,8,9,10 ?

Re: array sorting

Posted: Fri Nov 10, 2017 2:12 pm
by jPV
This should do the trick if all items do start with numbers:

Code: Select all

Sort(myImages$, Function(a, b) Return(Val(a) < Val(b)) EndFunction)
But that doesn't work too well if you have names starting with other than numbers too... maybe something like that for that kind of situation?

Code: Select all

Sort(myImages$, Function(a, b) If Val(a)=0 Or Val(b)=0 Then Return(a < b) Else Return(Val(a) < Val(b)) EndFunction)
Or does anyone come up with a better idea? :)

Re: array sorting

Posted: Fri Nov 10, 2017 2:42 pm
by sashapont
Thank you very much! It is work!