Array Sort issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an array that needs to be sorted but myArray.sort results in 2
following 19.
I've read something about that some time ago but can't find the solution. I
have to mention that my array is of type object since it might contain text
or numbers that it receive from another subroutine.
 
You need to implement IComparer to compare text to number.
Does the texts always represent a valid number?
 
Can it be really text or is it just numbers as text ? If the former, just
store numbers so that they are properly sorted. Else you'll need to
implement the ICompare interface to define how you would like compare those
elements.

As text, "19" is before "2" as "ai" is before "b"
 
Thanks, the input can be any characters (text only, numbers only or a mixture
- but I would still like to treat numbers as numebers)
Where can I get more info on implementing IComparer (I do not find it in my
offline help)
 
Paul,

Here's pseudo code for what you have to do and you don't need to implement
anything to sort an array. Initialize a comparison with the address of a
sort function to do the comparison. Maybe convert all the strings to numbers
in your sort function and do the comparison on a number.

dim s as Objects()
....fill array
Dim sorter As New Comparison(Of Object)(AddressOf xsorter)
Array.Sort(s, sorter)
....

Private Function xsorter(ByVal x As Object, ByVal y As Object) As Integer
if x>y return 1
if x<y return -1
return 0
End Function

Good Luck
DWS
 
DWS said:
Here's pseudo code for what you have to do and you don't need to implement
anything to sort an array.

You do if you're using .NET 1.1 (and want custom comparisons).
Comparison<T> only came in with .NET 2.0.

Unfortunately the OP hasn't said whether he's using 2.0 or not.
 
Back
Top