Page 1 of 1

Sort string array by numerical value

Posted: Sun Dec 29, 2013 12:19 pm
by PB
Inspired by this post...

http://www.purebasic.fr/english/viewtop ... 13&t=57817

...I came up with this procedure.

It sorts a string array by its numerical leading value,
instead of by its alphabetical leading value. It's okay
for small arrays, but horribly slow on large ones. :?

Team: A native flag to do this for SortArray() would be
really nice, such as #PB_Sort_Numeric. For the future?

Code: Select all

Procedure SortStringArrayByValue(Array tmp$(1))
  Repeat
    swapped=0
    For i=0 To ArraySize(tmp$())-1
      If Val(tmp$(i+1))<Val(tmp$(i))
        Swap tmp$(i+1),tmp$(i)
        swapped=1
        Break
      EndIf
    Next
  Until swapped=0
EndProcedure

Dim a$(3)

a$(0) = "100,test1.txt"
a$(1) = "10 test2.txt"
a$(2) = "200test3.txt"
a$(3) = "20   test4.txt"

SortStringArrayByValue(a$())

For i=0 To 3
  Debug a$(i)
Next

Re: Sort string array by numerical value

Posted: Mon Dec 30, 2013 2:55 pm
by Mistrel
PB wrote:It's okay for small arrays, but horribly slow on large ones.
Time to break out the quicksort? :)