Sort string array by numerical value

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Sort string array by numerical value

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Sort string array by numerical value

Post by Mistrel »

PB wrote:It's okay for small arrays, but horribly slow on large ones.
Time to break out the quicksort? :)
Post Reply