简体   繁体   中英

How to sort an array by a subvalue

I have found and modified the below code that works well but needs more modification that I cant figure out. I want it to sort randomly within the same value number. Basically what I want it to do is randomly sort everything but then group (or list) everything by value in desending order. For example you would have a name and a value but it might look smething like this. Name 2, name 2, name 2, name 3, name 4, name 4, name 4, name 5, name 8, name 8, ect.. Before it is over I could have about 500 names with about 8 0r 9 different values thus about 90 names in each value group

     Public Class Form1

Dim ListOfValues As New List(Of List(Of String))
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.txtA1.Focus()
End Sub

Private Sub AddTB(row As Integer, column As Integer, start As Char)
    Dim tb As New TextBox
    Dim offset As Integer = Math.Sign(Asc(start) - 65) * (100 + tb.Width * 3)
    tb.Name = "txt" & Chr(row + Asc(start)) & column.ToString
    tb.Text = tb.Name
    tb.Location = New Point(((column - 1) * tb.Width) + offset, (row * tb.Height))
    Me.Controls.Add(tb)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Button1.Enabled = True
    For I = 0 To 9
        ListOfValues.Add({Me.Controls("txt" & Chr(I + 65) & "1").Text, _
                          Me.Controls("txt" & Chr(I + 65) & "2").Text}.ToList)

    Next
    ListOfValues = ShuffleInfo(ListOfValues)
    'This fills the other textboxes with the data from the shuffled list
    For I = 0 To 9
        Me.Controls("txt" & Chr(I + 77) & "1").Text = ListOfValues(I)(0)
        Me.Controls("txt" & Chr(I + 77) & "2").Text = ListOfValues(I)(1)

    Next
End Sub

Private Function ShuffleInfo(ValuesToShuffle As List(Of List(Of String))) As List(Of List(Of String))
    'this follows the same basic routine you were using, swapping each item with a random item.
    Dim rand As New Random(Now.Millisecond)

    For counter = 0 To ValuesToShuffle.Count - 1
        Dim n = rand.Next(counter + 1)
        Dim temp As List(Of String) = ValuesToShuffle(counter)
        ValuesToShuffle(counter) = ValuesToShuffle(n)
        ValuesToShuffle(n) = temp
    Next
    ShuffleInfo = ValuesToShuffle
    Button1.Enabled = False
End Function
End Class

在此处输入图片说明

LINQ is handy here. See if this helps:

replace this line:

ListOfValues = ShuffleInfo(ListOfValues)

With this line:

ListOfValues = (From data In ShuffleInfo(ListOfValues)
                    Order By data(1) Descending).ToList

This will group the list by the second element of each item in the list, but keep the order random in each grouping.

From something like this:

C 3
B 1
C 1
B 2
C 2
A 3
A 2
B 3
A 1

You get this:

C 3
A 3
B 3
B 2
C 2
A 2
B 1
C 1
A 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM