|
-
Apr 13th, 2013, 12:04 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Apr 13th, 2013, 12:47 PM
#2
Re: Sorting an array and showing it in descending order
vb.net Code:
Private Sub LoadArray_And_ReturnGroup() '' Dim Array_List As New ArrayList With Array_List .Add("2") .Add("2") .Add("0") .Add("0") .Add("0") .Add("0") .Add("1") .Add("1") .Add("1") End With Dim Group_Count = From Lst In Array_List _ Group Lst By Lst Into Count() _ Select ax = Lst, GroupCount = Count Order By GroupCount Descending For Each Counts In Group_Count.ToList Me.TextBox1.Text += String.Concat(Counts.ax, " = ", Counts.GroupCount, " ; ") Next End Sub
Note :- More references can be found here
-
Apr 13th, 2013, 03:13 PM
#3
Thread Starter
Hyperactive Member
Re: Sorting an array and showing it in descending order
-
Apr 21st, 2013, 04:32 AM
#4
Thread Starter
Hyperactive Member
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?
-
Apr 21st, 2013, 05:09 AM
#5
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.
-
Apr 21st, 2013, 05:15 AM
#6
Thread Starter
Hyperactive Member
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.... and the program shows how many times the number "4" has appeared. But it seems that I'm doing it in the wrong way.
-
Apr 21st, 2013, 12:31 PM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|