简体   繁体   English

如何对姓名和号码进行排序?

[英]how to sort names and numbers?

vv VV

After completing my school assignment on simple array sorts I came up with this question. 在完成简单数组排序的学校作业后,我想到了这个问题。 Say I have a textbox with a name in it. 假设我有一个带有名称的文本框。 Right beside that I have a textbox with number in it. 在那旁边,我有一个带有数字的文本框。 Exp txtBox1 = "John Doe" , txtBox2 = 8 . Exp txtBox1 = "John Doe"txtBox2 = 8 Lets say I have 10 rows. 可以说我有10行。 that would be 20 text boxes. 那将是20个文本框。 How could I randomly sort these by name keeping all like numbers together in sequential order. 我如何才能按名称对这些相似的数字进行随机排序,将所有相似的数字按顺序排列在一起。 Output should look something like this. 输出应如下所示。 The key here is to randomly sort the names within the same number group. 此处的关键是对同一数字组中的名称进行随机排序。

  • John Doe 3 约翰·多伊3
  • Mary Jane 3 玛丽珍3
  • name 4 名字4
  • name 4 名字4
  • name 4 名字4
  • name 5 名字5
  • name 7 名字7
  • name 7 名字7
  • name 8 名字8
  • name 8 名字8

This is the code that I have. 这是我的代码。 it is slightly different in the fact that it has 3 column of textbox and 8 rows. 它具有3列文本框和8行的事实稍有不同。 This randomly sort the 3 rows keeping the information together in the same row. 这将对3行进行随机排序,将信息保持在同一行中。 John Doe, 3, phonenumber. John Doe,3岁,电话号码。 and then puts the information in a mirror image of textboxs. 然后将信息放入文本框的镜像中。 the number represents a skill level so I need alike skill levels to play alike skill levels but randomly sorted within there skill level. 该数字代表一个技能水平,因此我需要相同的技能水平才能发挥相同的技能水平,但要在该技能水平内随机排序。 Which this does not have. 这没有。 I cant having a 3 play a 7. I hope this makes since. 我不能让3打7。我希望从那时起。 Its almost as if I need a random order inside a sequencial order. 几乎就像我需要序列顺序中的随机顺序一样。

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

Something like this: 像这样:

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; }
}

EDIT: 编辑:
And here is the VB version 这是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

A slightly different version of Magnus' answer: 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

I think the others missed some important information from your post, if you want to know how to do this by sorting an array, here is what I would do. 我认为其他人错过了您的帖子中的一些重要信息,如果您想了解如何通过对数组进行排序来做到这一点,这就是我会做的。

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

This code assumes 3 textboxes, the 3rd allows multi-line and is expanded to show multiple lines. 此代码假定3个文本框,第3个允许多行,并被扩展为显示多行。 It also assumes a standard button to take the values from the 2 textboxes and add to your array. 它还假定使用一个标准按钮来从2个文本框中获取值并将其添加到数组中。

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

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