sorting - sortwith question

Hey!

I need to sort several arrays of the same type (text/string) so sortwith would be the ideal way.
Unfortunately, the array containing the data are properties of a class and don’t have a fixed count which
means a can’t add them to a param array at runtime. To be precise:

sample:
class columnTable
property - data(10) as Text

dim columns(2) as ColumnTable

columns(0).data.sortwith( columns(1).data, columns(2).data )

That works but what if I have 5 columns at runtime ?

please make an array with sort keys.
Than sort that array with keys with the other arrays.

Not elegant, but this should work:

Select Case columns.Ubound Case 0 columns(0).data.sort() Case 1 columns(0).data.sortwith( columns(1).data ) Case 2 columns(0).data.sortwith( columns(1).data, columns(2).data ) Case 3 columns(0).data.sortwith( columns(1).data, columns(2).data, columns(3).data ) Case 4 ... and so on

the columntables to be sorted are not fixed in count as I said. I mean the columnTable can have x elements. I cannot specify the paramArray.
I could only pass a variant with all arrays from each columnTable but this won’t work.

I think you’ll need to write a loop that sortwiths each columntable individually.

[code]Sub myColumnTableSort(tables() As ColumnTable)

dim base() As Text
//copy tables(0).data to base

for i As integer = 0 to tables.Ubound
dim temp() As Text
//copy base to temp
temp.SortWith(tables(i).data)
next

End Sub[/code]

If you’re concerned about efficiency you can combine this with Elis idea and do up to batches of 4 or so in the loop.

Thanks for all responses. I thought there is a simpler way to to that without copying data back and forth. My solution is basically a custom sortwith using a base array for the initial sort and an index array of integers to sort with.
The index array is later used to rearrange all other items to keep the data consistent as the
rows are tied like a listbox but only virtual. Assuming you sort ascending you can call sortByIndex rightaway, otherwise call reverse the index array and do the sort.

[code]Private Function arrayFromDown(indices() as integer) As integer()
dim c as integer= indices.Ubound
dim n(), idx as integer
redim n( c )

for i as integer = c downto 0
n( idx ) = indices( i )
idx=idx+1
next

return n

End Function[/code]

[code]Private Function sortByIndex(inData() as Text, sortindex() as integer) As Text()
dim c as integer = inData.Ubound
dim buff(-1) as Text
redim buff( c )
for index as integer=0 to c
buff(index) = inData( sortindex( index ) )
next

return buff
End Function[/code]

[code]Private Function getSorter(columnIndex as integer,type as SortType) As integer()
dim c as integer= self.rowCount-1
dim n() as integer
if c<=-1 then
return n
end if

redim n( c )
dim sortData() as Text
redim sortData( c )
dim targetColumn as ColumnTable = self.columns(columnIndex)

for idx as integer=0 to c
sortData(idx) = targetColumn.data(idx)
n( idx ) = idx
next

sortData.SortWith( n )

if type=SortType.Descending then
return self.arrayFromDown( n )
else
return n
end if
End Function[/code]

[code]Sub sort(columnIndex as integer, type as SortType)
if not (columnIndex>-1 and columnIndex<self.columnCount) then
self.setError( CurrentMethodName, 30 )
return
end if

dim indices() as integer = self.getSorter(columnIndex, type)
if indices.Ubound<0 then
return
end if

for each column as ColumnTable in self.columns
column.data = self.sortByIndex( column.data, indices )
next

End Sub[/code]

So you’re practically reordering all arrays yourself.