WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Arrays
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Secondary Sort on Columns

 Keywords: Array Sort ArraySort Primary Secondary Sort Column 

Question:

I am using 2010C, 6.10cjc and there appears to be a bug/feature in arraysort. I am sorting on column 0 ascending. e.g.
24 value1
25 value1
25 value2
25 value3
Result is
24 value1
25 value2
25 value3
25 value1

Answer:

Neither bug nor feature. 'ArraySort' does not make any promises about preserving the existing order of rows with the same sort column value. It simply sorts exclusively on the content of the user indicated array dimension in an efficient manor.

It is possible to perform a complete secondary, tertiary, etc. sorts using 'ArraySort's range parameters. It just takes a few extra lines of script to implement.

Crude example. Bugs included at no extra charge.

; Create test data
aTest = ArrDimension(4, 2)
nDim1 = ArrInfo(aTest, 1)
aTest[0,0] = 0
aTest[1,0] = 1
aTest[2,0] = 0
aTest[3,0] = 1
aTest[0,1] = 4
aTest[1,1] = 3
aTest[2,1] = 2
aTest[3,1] = 1

; Primary Sort on first column.
ArraySort(aTest, @ASCENDING, 0)

; Secondary sort on second column.
nMax1  = nDim1 - 2
nStart = 0
For i = 0 To nMax1
   If  aTest[i, 0] == aTest[i + 1, 0] Then Continue
   If nStart < i Then ArraySort(aTest, @ASCENDING, 1, nStart, i)
   nStart = i + 1
Next
If nStart < i Then   ArraySort(aTest, @ASCENDING, 1, nStart, i)

BreakPoint ; Check array contents in WB Studio.
Exit

Article ID:   W17667
Filename:   Secondary Sort on Columns.txt
File Created: 2010:09:09:12:46:38
Last Updated: 2010:09:09:12:46:38