Sort not working

G

Guest

Hi,

I tried to use the Sort() function of my ArrayList object to sort a list of
the following structures:

Public Structure myData
a as Long
b as Short
End Structure

Therefore, I wrote the following Comparer:

Public Class MyComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Dim one, two myData
one = CType(x, myData)
two = CType(y, myData)

Return Decimal.Compare(one.a, two.a) 'a is the interesting value
End Function
End Class

Now, when I call the sort function like that:

Dim x as new ArrayList
[...] Fill the arraylist
x.Sort(New MyComparer)

The values are sorted, beginning with the lowest value of 'a' at first
position and so on. What I'd really want is the biggest value at first
position...how could I do that?

Thanks,

Peter
 
J

Joanna Carter [TeamB]

"Peter Schmitz" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

| Therefore, I wrote the following Comparer:

....
|
| Now, when I call the sort function like that:
|
| Dim x as new ArrayList
| [...] Fill the arraylist
| x.Sort(New MyComparer)
|
| The values are sorted, beginning with the lowest value of 'a' at first
| position and so on. What I'd really want is the biggest value at first
| position...how could I do that?

Compare automatically sorts in ascending order; try negating the result of
the comparison :

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Dim one, two myData
one = CType(x, myData)
two = CType(y, myData)

Return -(Decimal.Compare(one.a, two.a)) 'a is the interesting
value
End Function
End Class

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top