Need to sort an Array

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

Hello,

I have an array that won't sort correctly:

Dim MyArrayList As New ArrayList

Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"

MyArrayList.AddRange(MyString .Split(","c))

MyArrayList .Sort()

For i As Integer = 0 To MyArrayList .Count - 2

Console.WriteLine(FormatNumber(MyArrayList .Item(i), 2, 0, 0, 0))

Next

I get:



1.00
2.00
20.00
22.00
3.00
32.50
40.00
44.00



I need:

1.00
2.00
3.00
20.00
22.00
32.50
40.00
44.00

Any help would be great!!!

Thanks in advance for you help,

Jack
 
Hello,

Thanks for your response, how do I get my ArrayList sorted correctly?

Thanks,

Jack
 
Jack said:
I have an array that won't sort correctly: [...]
Dim MyString As String = "5,3,44,22,20,2,40,1,32.50" [...]
I get:

1.00
2.00
20.00
22.00
3.00
32.50
40.00
44.00

Looks like it's sorted perfectly to me.

You declared the array to be of type String, so it performed an alphabetical
sort.

If you want it to sort numerically, you need to declare the array with a
numerical type, such as Integer, Double or Decimal.

HTH,
 
Hello, Jack,

However, if you really NEED the contents of the ArrayList to be Strings,
you could create your own comparer class. For example:

Public Class NumericComparer
Implements IComparer
Public Function Compare(ByVal x As Object, _
ByVal y As Object) As Integer _
Implements IComparer.Compare
Return CDbl(x) - CDbl(y)
End Function
End Class

Then replace your line:

MyArrayList.Sort()

with

Dim MyNumericComparer As New NumericComparer
MyArrayList.Sort(MyNumericComparer)

I think that will give you what you're looking for.

Note that this trivial comparer will throw an exception if any of your
ArrayList elements cannot be converted to double.

Cheers,
Randy

Jack said:
I have an array that won't sort correctly:
[...]

Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
[...]

I get:

1.00
2.00
20.00
22.00
3.00
32.50
40.00
44.00


Looks like it's sorted perfectly to me.

You declared the array to be of type String, so it performed an alphabetical
sort.

If you want it to sort numerically, you need to declare the array with a
numerical type, such as Integer, Double or Decimal.

HTH,
 
Thanks you very much!!!

This is just what I needed.

Jack

R. MacDonald said:
Hello, Jack,

However, if you really NEED the contents of the ArrayList to be Strings,
you could create your own comparer class. For example:

Public Class NumericComparer
Implements IComparer
Public Function Compare(ByVal x As Object, _
ByVal y As Object) As Integer _
Implements IComparer.Compare
Return CDbl(x) - CDbl(y)
End Function
End Class

Then replace your line:

MyArrayList.Sort()

with

Dim MyNumericComparer As New NumericComparer
MyArrayList.Sort(MyNumericComparer)

I think that will give you what you're looking for.

Note that this trivial comparer will throw an exception if any of your
ArrayList elements cannot be converted to double.

Cheers,
Randy

Jack said:
I have an array that won't sort correctly:
[...]

Dim MyString As String = "5,3,44,22,20,2,40,1,32.50"
[...]

I get:

1.00
2.00
20.00
22.00
3.00
32.50
40.00
44.00


Looks like it's sorted perfectly to me.

You declared the array to be of type String, so it performed an
alphabetical sort.

If you want it to sort numerically, you need to declare the array with a
numerical type, such as Integer, Double or Decimal.

HTH,
 
Hello Herfried K. Wagner [MVP],

DAMN. This is what I get for spewing crap without looking it up first.

-Boo
 
Back
Top