0

vv

在完成关于简单数组排序的学校作业后,我想出了这个问题。假设我有一个带有名称的文本框。在旁边,我有一个带有数字的文本框。经验txtBox1 = "John Doe"txtBox2 = 8。假设我有 10 行。那将是 20 个文本框。我怎么能按名称随机排序这些,将所有类似的数字按顺序排列在一起。输出应该看起来像这样。这里的关键是对同一号码组内的姓名进行随机排序。

  • 约翰·多伊 3
  • 玛丽珍 3
  • 名称 4
  • 名称 4
  • 名称 4
  • 名称 5
  • 名称 7
  • 名称 7
  • 名称 8
  • 名称 8

这是我拥有的代码。略有不同的是它有 3 列文本框和 8 行。这会随机对 3 行进行排序,将信息放在同一行中。John Doe,3 岁,电话号码。然后将信息放在文本框的镜像中。这个数字代表一个技能水平,所以我需要相同的技能水平才能发挥相同的技能水平,但在技能水平内随机排序。这是没有的。我不能有 3 打 7。我希望这能从那时起。就好像我需要一个顺序中的随机顺序。

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
    'this adds the data from the textboxes to the list.  each row of data is a list 
    'inside the list.  the controls collection can be indexed by control name.  
    'this makes it easy to access a specific control by using a naming pattern.  

    Button1.Enabled = True
    For I = 0 To 7
        ListOfValues.Add({Me.Controls("txt" & Chr(I + 65) & "1").Text, _
                          Me.Controls("txt" & Chr(I + 65) & "2").Text, _
                          Me.Controls("txt" & Chr(I + 65) & "3").Text}.ToList)
    Next
    ListOfValues = ShuffleInfo(ListOfValues)
    'This fills the other textboxes with the data from the shuffled list
    For I = 0 To 7
        Me.Controls("txt" & Chr(I + 83) & "1").Text = ListOfValues(I)(0)
        Me.Controls("txt" & Chr(I + 83) & "2").Text = ListOfValues(I)(1)
        Me.Controls("txt" & Chr(I + 83) & "3").Text = ListOfValues(I)(2)
    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
4

3 回答 3

4

像这样的东西:

void Main()
{
    var list = new List<Test>()
    { 
        new Test(){ Name = "John Doe", Value = 3 },
        new Test(){ Name = "Mary Jane", Value = 3 },
        new Test(){ Name = "Peter", Value = 3 },
        new Test(){ Name = "Arne", Value = 4 },
        new Test(){ Name = "Arne", Value = 4 }
    };

    var rand = new Random();
    var res = list.OrderBy(l => l.Value).ThenBy(l => rand.Next()).ToList();
    //Bind GridView/ListView with res as datasource here
}

public class Test
{
    public string Name { get; set; }
    public int Value { get; set; }
}

编辑:
这是 VB 版本

Private Sub Main()
    Dim list = New List(Of Test)
    list.Add(New Test("John Doe", 2))
    list.Add(New Test("Mary Jane", 3))
    list.Add(New Test("Peter", 4))
    list.Add(New Test("Arne", 5))

    Dim rand = New Random()
    list = list.
        OrderBy(Function(l) l.Value).
        ThenBy(Function(l) rand.Next()).
        ToList()
End Sub

Public Class Test
    Public Sub New(name As String, value As Int32)
        Me.Name = name
        Me.Value = value
    End Sub
    Public Property Name As String
    Public Property Value As Int32
End Class
于 2013-10-02T15:16:52.520 回答
0

我认为其他人错过了您帖子中的一些重要信息,如果您想知道如何通过对数组进行排序来做到这一点,这就是我会做的。

Private sList As String() = Array.CreateInstance(GetType(String), 0)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Resize the array
    ReDim Preserve sList(sList.Length)
    'Concatenant the 2 strings and put them into the array
    sList(sList.Length - 1) = TextBox1.Text & " " & TextBox2.Text
    'Sort the array of strings
    Array.Sort(sList)
    'Put the array into a StringBuilder so we can display in a 3rd textbox
    Dim SB As New System.Text.StringBuilder()
    For Each s As String In sList
        SB.AppendLine(s)
    Next
    'Display the text
    TextBox3.Text = SB.ToString
End Sub

这段代码假定有 3 个文本框,第 3 个允许多行并扩展为显示多行。它还假定一个标准按钮从 2 个文本框中获取值并添加到您的数组中。

于 2013-10-02T18:56:25.223 回答
0

Magnus 的回答略有不同:

Dim list As New List(Of KeyValuePair(Of String, Integer)) From
                          {
                            New KeyValuePair(Of String, Integer)("John Doe", 8),
                            New KeyValuePair(Of String, Integer)("Mary Jane", 3),
                            New KeyValuePair(Of String, Integer)("Mary Jane", 5),
                            New KeyValuePair(Of String, Integer)("Peter", 6),
                            New KeyValuePair(Of String, Integer)("Arne", 5)
                          }

Dim rand as New Random()
' Note: if you don't want them sorted alphabetically by name, 
' then omit "item.Key," from the Order By clause.   
list = (From item In list
        Select item
        Order By item.Value, item.Key, rand.Next).ToList
于 2013-10-02T16:04:10.917 回答