I need my sorted list to sort Desc, instead of Ascending

Friends,

I have my code below...

I am wanting to display my data in Descending order instead of Asending.  Any Ideas?

Thanks in advance!

Best Regards,
Eric
Dim EntrantPoints As SortedList(Of Double, String)    
 
Private Sub ListEntrantPoints()
        Dim li As ListViewItem
        'EntrantPoints.Reverse()
        For Each SortedValue In EntrantPoints
            Dim SplitUpString() As String
            SplitUpString = Split(SortedValue.Value.ToString, "-")
            li = lvPitOrder.Items.Add(SplitUpString(0))
            li.SubItems.Add(SplitUpString(1))
            li.SubItems.Add(SplitUpString(2))
            li.SubItems.Add(SplitUpString(3))
        Next
    End Sub

Open in new window

indy500fanAsked:
Who is Participating?
 
Wayne Taylor (webtubbs)Commented:
What about looping through the SortedList in reverse??
    Dim EntrantPoints As SortedList(Of Double, String)
 
    Private Sub ListEntrantPoints()
        Dim li As ListViewItem
        For i As Integer = EntrantPoints.Count - 1 To 0 Step -1
            Dim SplitUpString() As String
            SplitUpString = EntrantPoints.Values(i).ToString.Split("-"c)
            li = lvPitOrder.Items.Add(SplitUpString(0))
            li.SubItems.Add(SplitUpString(1))
            li.SubItems.Add(SplitUpString(2))
            li.SubItems.Add(SplitUpString(3))
        Next
    End Sub

Open in new window

0
 
vs1784Commented:
In short you need to use the SortedList constructor that takes an IComparer as in argument.
Then just pass in an IComparer that sorts your items in the order that you want.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
indy500fanAuthor Commented:
Uh, okay.  I'm never very good at converting C#.  How do I apply this to my example?  
0
 
vs1784Commented:
0
 
indy500fanAuthor Commented:
Everyone.  Maybe I should have been more specific.  Can I get specific code help with my example?

I've looked at these but I can't figure out how to implement.  For 500 points, I am looking for help with my example rather than links.

Best Regards,
Eric
0
 
vs1784Commented:
What type of data is being stored in sortedlist ? string, int?
0
 
indy500fanAuthor Commented:
I do like that conversion tool that's neat!.  When I converted suggestions to vb, I guess I'm not sure how to implement in my example.
0
 
indy500fanAuthor Commented:
vs1784, the key is double and the value is a string
0
 
indy500fanAuthor Commented:
"In short you need to use the SortedList constructor that takes an IComparer as in argument.
Then just pass in an IComparer that sorts your items in the order that you want."

Okay.  How do I do that?
0
 
indy500fanAuthor Commented:
It sounds similar to what you had in the example you sent in your first suggestion.  I tried you code converter to try and implement, but could not get it to work.
0
 
indy500fanAuthor Commented:
Bingo!

That'll work!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi indy500fan,

I've been forcing myself to learn LINQ and thought your question would be a good  candidate!  LINQ is available in VB.Net 2008 (.Net 3.0 and above)...

So below is an alternate solution for you presented in a simple example.

Here is the output:

Default Order: (Ascending)
1 --> D
2 --> C
3 --> B
3.5 --> B+
4 --> A
Reversed Order: (Descending)
4 --> A
3.5 --> B+
3 --> B
2 --> C
1 --> D

Public Class Form1
 
    Dim EntrantPoints As New SortedList(Of Double, String)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        EntrantPoints.Add(2.0, "C")
        EntrantPoints.Add(3.0, "B")
        EntrantPoints.Add(1.0, "D")
        EntrantPoints.Add(4.0, "A")
        EntrantPoints.Add(3.5, "B+")
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Debug.Print("Default Order: (Ascending)")
        For Each pair In EntrantPoints
            Debug.Print(pair.Key & " --> " & pair.Value)
        Next
 
        Dim reversed = From p As KeyValuePair(Of Double, String) In EntrantPoints Order By p.Key Descending Select p
        Debug.Print("Reversed Order: (Descending)")
        For Each pair In reversed
            Debug.Print(pair.Key & " --> " & pair.Value)
        Next
    End Sub
 
End Class

Open in new window

0
 
indy500fanAuthor Commented:
Idle Mind,

It's been a while.  Your solution looks interesting.  I'll have to try it in my implemetation.
0
 
Wayne Taylor (webtubbs)Commented:
Idle_Mind - that looks pretty cool! I'll have to look into LINQ myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.