Macro SubSortStructuredArray(Arr, MainField, SubOptions, SubOffset, SubType) Define SubSort_Start, SubSort_I SubSort_Start = 0 For SubSort_I = 1 To ArraySize(Arr()) If Arr(SubSort_Start)\MainField <> Arr(SubSort_I)\MainField If SubSort_Start - (SubSort_I - 1) <> 0 ; Performance optimization ; Debug Str(SubSort_Start) + ", " + Str(subsort_i-1) SortStructuredArray(Arr(), SubOptions, SubOffset, SubType, SubSort_Start, SubSort_I-1) EndIf SubSort_Start = SubSort_I EndIf Next ; Debug Str(SubSort_Start) + ", " + Str(subsort_i-1) SortStructuredArray(Arr(), SubOptions, SubOffset, SubType, SubSort_Start, SubSort_I-1) EndMacro ;- Example Structure myABC a$ b$ c$ EndStructure Define.i i, tw = 4 #max = 17 Dim myL.myABC(#max) ; Fill array For i = 0 To #max Read.s myL(i)\a$ Read.s myL(i)\b$ Read.s myL(i)\c$ Next ; Print before sort Debug "-- Before Sort --" Debug LSet("a$", tw) + LSet("b$", tw) + LSet("c$", tw) Debug LSet("--", tw) + LSet("--", tw) + LSet("--", tw) For i = 0 To #max Debug LSet(myL(i)\a$, tw) + LSet(myL(i)\b$, tw) + LSet(myL(i)\c$, tw) Next ; Primary sort on b$, secondary sort on c$, tertiary sort on $a SortStructuredArray(myL(), #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(myABC\b$), #PB_Sort_String) SubSortStructuredArray(myL, b$, #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(myABC\c$), #PB_Sort_String) ; ^ This is the key was previously sorted ^ This is the key that should be sorted next SubSortStructuredArray(myL, c$, #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(myABC\a$), #PB_Sort_String) ; ; Primary sort on c$, secondary sort on b ; SortStructuredArray(myL(), #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(myABC\c$), #PB_Sort_String) ; SubSortStructuredArray(myL, c$, #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(myABC\b$), #PB_Sort_String) ; ; ^ This is the key was previously sorted ^ This is the key that should be sorted next ; ; Note: due to the nature of the example data, secondary sort on $b after sort on $c does nothing. ; Print after sort Debug "-- After Sort on $b, $c, $a --" Debug LSet("a$", tw) + LSet("b$", tw) + LSet("c$", tw) Debug LSet("--", tw) + LSet("--", tw) + LSet("--", tw) For i = 0 To #max Debug LSet(myL(i)\a$, tw) + LSet(myL(i)\b$, tw) + LSet(myL(i)\c$, tw) Next DataSection SortThis: ; 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 "z", "3", "z" Data.s "d", "3", "y" Data.s "n", "3", "x" Data.s "o", "5", "x" Data.s "n", "5", "x" Data.s "1", "3", "a" Data.s "2", "3", "a" Data.s "3", "3", "a" Data.s "1", "3", "x" Data.s "3", "3", "x" Data.s "1", "3", "y" Data.s "2", "3", "y" Data.s "3", "3", "y" Data.s "4", "3", "d" EndDataSection