skywalk wrote:
Sorry to bump this topic, but I have a simple example that screams for MergeSort.
I'd like to keep the data in an array if possible, but the SortStructuredArray() function jumbles the order(unstable) of equal data.
Maybe we could get Merge option for Structured Arrays also?
I have looked at a lot of my data structures, and very few are truly unweighted or independent of related fields.
And what did Fred mean about about range sorting?
Fred meant that if you are sorting on field 1, then on field 2 that you would first sort field 1 then resort the sub-ranges on field 2 where the field 1 values are equal. In other words if you had 3 entrys where the first field equaled "A" then you would sort the subrange of 1 to 3 for field 2.
Here is some example code that uses an include that generalizes multi-field sorting for a single structure, see link in the Edit comment for a copy of the include:
Code:
;Author: Demivec
EnableExplicit
IncludeFile "Array_MultiFieldSort.pbi"
;-setup
Structure myABC
a$
b$
c$
EndStructure
Define i, entryCount
Restore SortThis
Read.i entryCount
Dim myL.myABC(entryCount)
For i = 0 To ArraySize(myL())
Read.s myL(i)\a$
Read.s myL(i)\b$
Read.s myL(i)\c$
Next
Procedure display_myABC(Array x.myABC(1), header.s = "", footer.s = "")
Protected i, tw = 4
Debug header
Debug LSet("a$", tw) + LSet("b$", tw) + LSet("c$", tw)
Debug LSet("--", tw) + LSet("--", tw) + LSet("--", tw)
For i = 0 To ArraySize(x())
Debug LSet(x(i)\a$, tw) + LSet(x(i)\b$, tw) + LSet(x(i)\c$, tw)
Next
Debug footer
EndProcedure
display_myABC(myL(), "-- Before Sort --")
;-sort routines
mcr_multiSort(myABC)
;-sort
Dim myl_sortspecs.sortFieldSpecs(1) ;Array should be sized to be one less than the number of fields to be sorted
mcr_setSortSpec(myl_sortspecs, 0, OffsetOf(myABC\b$), #PB_Sort_String, #PB_Sort_Ascending)
mcr_setSortSpec(myl_sortspecs, 1, OffsetOf(myABC\c$), #PB_Sort_String, #PB_Sort_Ascending)
multiSort_myABC(myL(), myl_sortspecs())
display_myABC(myL(), "-- Sort by: b$ A, then c$ A --")
DataSection
SortThis:
Data.i 10 ;0 based entry count
; a$, b$, c$
Data.s "a", "2", "5"
Data.s "a", "2", "1"
Data.s "x", "1", "b"
Data.s "y", "1", "a"
Data.s "a", "3", "5"
Data.s "z", "3", "z"
Data.s "a", "2", "1"
Data.s "d", "3", "y"
Data.s "y", "1", "c"
Data.s "y", "1", "b"
Data.s "n", "3", "x"
EndDataSection
Here is an example that uses a structure containing different element types and sorts all 4 fields, some in Ascending and some in Descending order:
-- Code Removed -- See comment regarding edit.
The code to perform the multi-field sort is currently limited to handling the sorting of fields for a particular structure, as can be seen by comparing the two code samples. Even with that limitation it is generalized to handle any number of fields each of a different standard PB variable type in the given structure (excluding arrays, lists, and maps). I constructed the code by lifting some routines from other code I have written. I only had time to generalize some of it. If more time was to be found the sorting code could be further generalized to handle any structure.
I hope you find it useful.

If I generalize this further I'll repost the code to the Tips & Tricks forum and remove it from here.
@Edit: Code removed as predicted and after being refined a little bit, posted
here.