sorting alphanumeric values
I am having trouble getting my code to sort a ArrayList of component ID'd as such:
R345
C20
IC78
R89
C139
R56
And everything I try comes up with:
C139
C20
IC78
R345
R89
R56
Whereas I need it like:
C20
C139
IC78
R56
R89
R345
I have tried ArrayList.Sort(), and even splitting the characters from the integers, and trying a bubble sort, but to no avail.
I hope someone can point me in the right direction, perhaps i've missed something ovbious.
Thanks for your help in advance.
Nellie
[627 byte] By [
nellie] at [2007-11-11 6:53:31]

# 1 Re: sorting alphanumeric values
You'll have to create a custom comparer which compares the IDs based on their numeric values following the first character. For more information, see http://www.vb-helper.com/howto_net_custom_sort_array.html
# 2 Re: sorting alphanumeric values
i have this, which should work:
Dim i, n, j As Integer
Dim tmp As String
n = IDArr.Count
For i = 0 To i < n - 3 Step 2
For j = 0 To j < n - 3 - i Step 2
If IDArr.Item(j + 2) < IDArr.Item(j) Then
'swap them!
tmp = IDArr.Item(j)
IDArr.Item(j) = IDArr.Item(j + 2)
IDArr.Item(j + 2) = tmp
tmp = IDArr.Item(j + 1)
IDArr.Item(j + 1) = IDArr.Item(j + 3)
IDArr.Item(j + 3) = tmp
End If
Next
Next
For i = 0 To i < n - 3 Step 2
For j = 0 To j < n - 3 - i Step 2
If IDArr.Item(j + 3) = IDArr.Item(j + 1) Then
If Convert.ToInt64(IDArr.Item(j + 3)) < Convert.ToInt64(IDArr.Item(j + 1)) Then
'swap them!
tmp = IDArr.Item(j)
IDArr.Item(j) = IDArr.Item(j + 2)
IDArr.Item(j + 2) = tmp
tmp = IDArr.Item(j + 1)
IDArr.Item(j + 1) = IDArr.Item(j + 3)
IDArr.Item(j + 3) = tmp
End If
End If
Next
Next
End Function
where the id numbers are split before being put into the array, as thus:
array(0) = R
array(1) = 678
array(2) = C
array(3) = 23
being R678 and C23
nellie at 2007-11-11 21:50:57 >
