Page 1 of 1

SortArray extra flag

Posted: Wed Jun 10, 2009 2:41 pm
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). ;)

Posted: Wed Jun 10, 2009 4:21 pm
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.

Posted: Wed Jun 10, 2009 5:31 pm
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