Results 1 to 7 of 7

Thread: [RESOLVED] Sorting an array and showing it in descending order

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Resolved [RESOLVED] Sorting an array and showing it in descending order

    I've the next variable:
    Code:
     Dim MyNumbers(0 to 50) as Integer
    In a textbox I introduce numbers from 0 to 50 and I want that in a TextBox appears the most repeated 10 numbers in a descending order.

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Sorting an array and showing it in descending order

    vb.net Code:
    1. Private Sub LoadArray_And_ReturnGroup()
    2.         ''
    3.         Dim Array_List As New ArrayList
    4.  
    5.         With Array_List
    6.  
    7.             .Add("2")
    8.             .Add("2")
    9.  
    10.             .Add("0")
    11.             .Add("0")
    12.             .Add("0")
    13.             .Add("0")
    14.  
    15.             .Add("1")
    16.             .Add("1")
    17.             .Add("1")
    18.  
    19.         End With
    20.  
    21.  
    22.         Dim Group_Count = From Lst In Array_List _
    23.                           Group Lst By Lst Into Count() _
    24.                           Select ax = Lst, GroupCount = Count Order By GroupCount Descending
    25.  
    26.  
    27.         For Each Counts In Group_Count.ToList
    28.  
    29.             Me.TextBox1.Text += String.Concat(Counts.ax, " = ", Counts.GroupCount, " ; ")
    30.  
    31.         Next
    32.  
    33.     End Sub
    Note :- More references can be found here
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: Sorting an array and showing it in descending order

    Thanks!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: [RESOLVED] Sorting an array and showing it in descending order

    I'm trying to show the list, but this time I want to show in the list the numbers in the same descending order and the amount of times they have been appeared.
    Example:

    Number 5 -> 45 times
    Number 6-> 32 times
    Number 17 ->19 times
    etc..

    I've tried this but it seems it doesn't works:
    Code:
            For Each Counts In Group_Count.ToList
                Me.Label1.Text += String.Concat(Counts.ax, " (", MyNumberMatrix.Item(Counts.ax), ")", vbCrLf)
            Next
    How can I solve that?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Sorting an array and showing it in descending order

    Doesn't the code already provided do just what you say you're trying to do? If you want to format the output text differently then that's just a minor detail.

    That said, if you don't understand the LINQ code and don't really understand what it's replacing then maybe doing it the old-fashioned way first would be a better idea. Before the advent of LINQ, this would have been done with a loop and it still can be. You could start with a SortedList and then loop through your original values. For each value, you can first check whether the list contains the value. If it doesn't then you add it and initialise the corresponding value to 1. If it does then you increment the value by 1. At the end of the loop, the distinct original values are ordered in the keys of the SortedList and the counts are in the corresponding values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: [RESOLVED] Sorting an array and showing it in descending order

    The code posted by make me rain works as desired. The only thing I want to change this time is to add a new thing to the original code.
    When I introduce N number of values, the code sort the arraylist in descending order and it's updated each time a new element is added to the arraylist. At the moment I want only add a piece of code that shows how many times the number has appeared.
    I thought it was something like....
    Code:
     Array_List.Item(4)
    and the program shows how many times the number "4" has appeared. But it seems that I'm doing it in the wrong way.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: [RESOLVED] Sorting an array and showing it in descending order

    I've resolved the problem using the next modification:

    Code:
    Dim TimesNumberHasAppeared (0 to 20) as integer
    ____________________________________________
    
    For Each Counts In Group_Count.ToList
     
                Me.TextBox1.Text += String.Concat(Counts.ax, " (", TimesNumberHasAppeared(Counts.ax), ")")
     
    Next
    The final format looks like:
    5 (8) 17 (4) 3 (2) ....

    Where the first number is the number itself, and the number between () is the number of times that number has appeared. Each time I enter a new value the list refreshes and sorts in descending order, from the most repeated one, in this case the number 5, to the less repeated one, in this case 3. The numbers that haven't appeared are omitted.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width