Sort pair values using numeric column

  • Thread starter Thread starter Dot net work
  • Start date Start date
D

Dot net work

I would like to keep track of pairs of values, like this:

Apple 50
Pear 100
Banana 30

I would like to keep this data sorted using the number column, and not
the text column.

So, I need this:

Pear 100
Apple 50
Banana 30

Please can someone recommend a good .NET framework "structure" that
would help me achieve this.

TIA,
-dnw.
 
Take a look athe System.Collections.SortedList class. Add the number as the
key and the string as the item and you should have your items sorted.

eg. Dim oSortedList as New SortedList
oSortedList.Add(50, "Apple")
oSortedList.Add(100, "Pear"), etc

hope this helps..
Imran.
 
Thanks a lot.

As a finishing touch, I added the following:

'Make a comparer class that can see if an integer is greater or smaller
than another integer...

Private Class IntComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare
Dim x1 As Integer
Dim x2 As Integer

x1 = Convert.ToInt32(x)
x2 = Convert.ToInt32(y)

If x1 < x2 Then Return 1 Else Return -1
End Function
End Class

'Next, make a sorted list, and use the above comparer class.

Dim oSL As New SortedList(New IntComparer())

'Then, in this particular case, your items are sorted with the highest
integer at the top (which is what I need)

So, if you have

50 Apple
100 Pear

Your list is sorted as:

100 Pear
50 Apple

Thanks, dnw.
 

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

Back
Top