Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

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

Avatar of vs1784
vs1784

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.
Avatar of indy500fan

ASKER

Uh, okay.  I'm never very good at converting C#.  How do I apply this to my example?  
Avatar of Dirk Haest
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
What type of data is being stored in sortedlist ? string, int?
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.
vs1784, the key is double and the value is a string
"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?
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.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bingo!

That'll work!
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

Idle Mind,

It's been a while.  Your solution looks interesting.  I'll have to try it in my implemetation.
Idle_Mind - that looks pretty cool! I'll have to look into LINQ myself.