array sorting

Discuss any general programming issues here
Post Reply
sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

array sorting

Post by sashapont » Fri Nov 10, 2017 1:30 pm

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 ?

User avatar
jPV
Posts: 156
Joined: Sat Mar 26, 2016 11:44 am
Location: RNO
Contact:

Re: array sorting

Post by jPV » Fri Nov 10, 2017 3:12 pm

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? :)

sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

Re: array sorting

Post by sashapont » Fri Nov 10, 2017 3:42 pm

Thank you very much! It is work!

Post Reply