简体   繁体   English

如何按子值对数组排序

[英]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 名称2,名称2,名称2,名称3,名称4,名称4,名称4,名称5,名称8,名称8等。在结束之前,我可以拥有约500个名称,其中约8个0r 9个不同的值因此,每个值组中大约有90个名称

     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. LINQ在这里很方便。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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