# sorting - sortwith question

**URL:** <https://forum.xojo.com/t/sorting-sortwith-question/29459>\
**Category:** General\
**Created:** [January 29, 2016, 9:04am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459 "2016-01-29T09:04:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_Egal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rob\_Egal](https://forum.xojo.com/u/Rob_Egal)\
**Post date:** [January 29, 2016, 9:04am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/1 "2016-01-29T09:04:06Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [January 29, 2016, 9:39am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/2 "2016-01-29T09:39:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [January 29, 2016, 9:41am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/3 "2016-01-29T09:41:49Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Rob\_Egal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rob\_Egal](https://forum.xojo.com/u/Rob_Egal)\
**Post date:** [January 29, 2016, 10:29am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/4 "2016-01-29T10:29:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [January 29, 2016, 10:50am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/5 "2016-01-29T10:50:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rob\_Egal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rob\_Egal](https://forum.xojo.com/u/Rob_Egal)\
**Post date:** [January 29, 2016, 11:36am UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/6 "2016-01-29T11:36:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:10pm UTC](https://forum.xojo.com/t/sorting-sortwith-question/29459/7 "2020-10-29T18:10:00Z")

</div>


