Sorting a two-dimensional array

  • Thread starter Thread starter moondog
  • Start date Start date
M

moondog

How do you sort a 2D array?

Dim myArray(10000, 9) As String

myArray contains 10000 records with 9 fields. I want to sort on the
Ninth field.


I want to be able to use the sort method, but how?

myArray.sort( ?, ?)


Thanks,
Moondog
 
Hi Moondog,

The Sort method only works on single dimension arrays. Rather than using a
two dimensional array, you could use a jagged array, e.g:

Dim myArray(1000)() As String

' populate the array, ensuring there are 0 to 9 items in each slot
' the sort
Array.Sort(s, New Comparison(Of String())(AddressOf JaggedArrayComparer))



Private Function JaggedArrayComparer(ByVal item1 As String(), ByVal item2
As String()) As Int32
Return item1(9).CompareTo(item2(9))
End Function


Regards,

Bill.
 
Back
Top