Here's an example using qsort.
It might be needed to extend the lookup table to unicode values between 256 and 591.
Code: Select all
; import qsort procedure
ImportC ""
qsort(*base, num, size, *comparator)
EndImport
; lookup table
DataSection
LUT_Compare:
Data.u 64,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,119,120,121,122,91,92,93,94,95,
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
97,97,97,97,97,97,230,99,101,101,101,101,105,105,105,105,
240,110,111,111,111,111,111,215,248,117,117,117,117,121,254,223,
97,97,97,97,97,97,230,99,101,101,101,101,105,105,105,105,
240,110,111,111,111,111,111,247,248,117,117,117,117,121,254,121
EndDataSection
; create global lookup array
Global Dim LUT_Compare.u(191)
CopyMemory(?LUT_Compare, @LUT_Compare(), 384)
; compare procedure
ProcedureC.i Compare(*s1.String, *s2.String)
Protected c1.c, c2.c, result.i
Protected *c1.Character = @*s1\s
Protected *c2.Character = @*s2\s
If *c1 = 0
If *c2 = 0
ProcedureReturn 0 ; Both pointers 0
Else
ProcedureReturn -1 ; First pointer 0, second not
EndIf
ElseIf *c2 = 0
ProcedureReturn 1 ; Second pointer 0, first not
Else
; Both valid strings so compare
Repeat
c1 = *c1\c : *c1 + SizeOf(Character)
c2 = *c2\c : *c2 + SizeOf(Character)
If c1 >= 64 And c1 < 192 : c1 = LUT_Compare(c1 - 64) : EndIf
If c2 >= 64 And c2 < 192 : c2 = LUT_Compare(c2 - 64) : EndIf
result = c1 - c2
Until result Or c1 = 0
ProcedureReturn result
EndIf
EndProcedure
; array to sort
Dim values.s(2)
values(0)="até"
values(1)="atão"
values(2)="Luís"
; perform sort
qsort(@values(), ArraySize(values()) + 1, SizeOf(String), @Compare())
; present result
For i = 0 To 2
Debug values(i)
Next