SortArray extra flag

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

SortArray extra flag

Post by PB »

If there is no performance hit, would it be easy to add an extra flag to the
SortArray command, for string arrays only, that specify which character to
start sorting from? For example, consider this string array:

Code: Select all

MyArray$(1)="zzzbbb"
MyArray$(2)="yyyccc"
MyArray$(3)="xxxaaa"
My dream is to be able to do this:

Code: Select all

SortArray(MyArray(),#PB_Sort_Ascending,1,3,4)
The last number, 4, is the new flag, and tells the array to be sorted according
to the 4th character and remaining. So if this command worked, the new array
would become this:

Code: Select all

MyArray$(1)="xxxaaa"
MyArray$(2)="zzzbbb"
MyArray$(3)="yyyccc"
Why did I ask for this? Because I want to sort an array of filenames by
their name only, and not their path, but their path stops the sort from
working correctly. Currently I have to split each array (with GetFilePart)
and store their paths separately in another array, then sort and reattach
the paths to the start of the sorted names. Yucky.

(PS. RandomizeArray() for strings and numerics would be nice, too). ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Currently I have to split each array (with GetFilePart)
and store their paths separately in another array, then sort and reattach
the paths to the start of the sorted names. Yucky.
Why would you want to store them together in the first place?

In any case, their paths must have the same length for this work. That's highly unlikely to be the case, unless it's actually the same path. In this case, just use a regular sort.
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Post by Marco2007 »

What about something like this?

Code: Select all

Dim MyArray$(5)

Procedure.s new(name.s, flag)
  ProcedureReturn Mid(name,flag)+#TAB$+name
EndProcedure

MyArray$(0)=new("yyyfff",4)
MyArray$(1)=new("zzzbbb",4)
MyArray$(2)=new("yyyccc",4)
MyArray$(3)=new("xxxaaa",4)
MyArray$(4)=new("yyyddd",4)
MyArray$(5)=new("xxxzzz",4)

SortArray(MyArray$(), #PB_Sort_Ascending)

For i=0 To 5
  Debug StringField(MyArray$(i), 2, #TAB$)
Next

xxxaaa
zzzbbb
yyyccc
yyyddd
yyyfff
xxxzzz
PureBasic for Windows
Post Reply