How do I sort in decending order with Visual Basic 2010?

Hello experts.  I was given a program that needs to sort in Descending order.  I understand that the lstBox.Sorted = True is only for Ascending order but how do I reverse the order?  Here is my code so far.  Any help is appreciated.  

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    Dim voting = IO.File.ReadAllLines("Votes.txt")
    Dim voteQuery = From vote In votes
                    Select vote
                    Distinct
    Dim votes() As String = voteQuery.ToArray
    Dim w As Integer = 0
    Dim count As Integer = 0
    For Each vote As String In votes
      Dim t As Integer = 0
      For i As Integer = 0 To voting.Count - 1
        If votes(w) = votes(t) Then
          count += 1
          t += 1
        Else
          t += 1
        End If
      Next
      lstBox.Sorted = True
      lstBox.Items.Add(count & "  " & votes(w))
      w += 1
      count = 0
    Next
  End Sub
End Class
voyager1845Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
I see you're using a Linq query. Why not just sort that? Then you can add the items sequentially as you are already doing and they will be sorted. Set "lstBox.Sorted" to False, though.
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    Dim voting = IO.File.ReadAllLines("Votes.txt")
    Dim voteQuery = From vote In votes
                    Select vote
                    Distinct
                    Order By vote Descending
    Dim votes() As String = voteQuery.ToArray
    Dim w As Integer = 0
    Dim count As Integer = 0
    For Each vote As String In votes
      Dim t As Integer = 0
      For i As Integer = 0 To voting.Count - 1
        If votes(w) = votes(t) Then
          count += 1
          t += 1
        Else
          t += 1
        End If
      Next
      lstBox.Items.Add(count & "  " & votes(w))
      w += 1
      count = 0
    Next
  End Sub
End Class

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, Im not sure if this is what you want to do, but try it:
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim voteList As New List(Of String)

        Dim votes() As String = IO.File.ReadAllLines("Votes.txt")
        Dim w As Integer = 0
        Dim count As Integer = 0
        For Each vote As String In votes
            Dim t As Integer = 0
            For i As Integer = 0 To votes.Length - 1
                If votes(w) = votes(t) Then
                    count += 1
                    t += 1
                Else
                    t += 1
                End If
            Next
            voteList.Add(count & "  " & votes(w))
            w += 1
            count = 0
        Next

        ' Sort your list with MyDescComparer class before display it
        voteList.Sort(New MyDescComparer)

        lstBox.DataSource = voteList
    End Sub

    Private Class MyDescComparer
        Implements IComparer(Of String)
        Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements IComparer(Of String).Compare
            Return String.Compare(y, x)
        End Function
    End Class

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi voyager1845;

Looking at the code below I believe you have an issue with the query because you are defining the sequence after the query and then place the returned sequence in the variable you are querying.

Dim voting = IO.File.ReadAllLines("Votes.txt")

Dim voteQuery = From vote In votes
                Select vote
                Distinct

Dim votes() As String = voteQuery.ToArray

Dis you mean this?

Dim voting = IO.File.ReadAllLines("Votes.txt")

Dim voteQuery = From vote In voting
                Select vote
                Distinct

Dim votes() As String = voteQuery.ToArray

Also the suggestion made by kaufmed would be the better way to go.

Fernando
0
 
Javin007Commented:
Do you want the easy answer, or the "best" answer?

The "Best" answer is to create a sorting algorithm, sort the data in an array upon load, and put it into your form.

The EASY answer is the following:

Send a windows message to the control telling it not to "redraw" itself
Create two columns in the control, one that the data will go in, one that's blank.
Sort the columns ascending on the column you want to sort.
In the other column, add numbers in reverse order.
Sort the control again, but this time on the extra column.
Hide the extra column.
Send a windows message to the control telling it to go ahead and redraw.
Refresh the control.
0
 
voyager1845Author Commented:
I apologize for the delay in responding to this posting.  I took kaufmed suggestion and redid my code.  This is what I came up with that works.  It’s much simpler that what I was originally doing.  Thank you for all the responses and suggestions.  

Public Class frmVoters

  Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    Dim Voters = From line In IO.File.ReadAllLines("Voters.txt")
      Where (line.Trim.Length > 0)
      Group line By line Into Count()
      Order By Count Descending, line Ascending
    lstOutput.Items.Clear()
    For Each Vote In Voters
      lstOutput.Items.Add(Vote.Count & "              " & Vote.line & "")
    Next
  End Sub
End Class
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.