?
Solved

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

Posted on 2011-04-23
5
Medium Priority
?
512 Views
Last Modified: 2013-12-16
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
0
Comment
Question by:voyager1845
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35455068
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
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35455155
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35456309
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
 
LVL 4

Expert Comment

by:Javin007
ID: 35477796
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
 

Author Comment

by:voyager1845
ID: 35712897
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question