如何在 VB.net 中对数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23815819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:35:31  来源:igfitidea点击:

How can I sort an array in VB.net?

vb.net

提问by user3666449

Hello guys am working on a window application in vb.net Am trying to code an array that can add only 8 best subjects grades from the 10 subjects done by a student. the ten subject grades are displayed as labels and also the TOTAL of best eight subject grades is displayed as a label. Any one to help me. am new in programming. Here is my code.

大家好,我正在 vb.net 中开发一个窗口应用程序,我正在尝试编写一个数组,该数组只能从学生完成的 10 个科目中添加 8 个最佳科目成绩。十个学科成绩显示为标签,并且最好的八个学科成绩的总和显示为一个标签。任何人来帮助我。我是编程新手。这是我的代码。

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

    ' subject grades
    Label1.Text = 7
    Label2.Text = 1
    Label3.Text = 3
    Label4.Text = 8
    Label5.Text = 4
    Label6.Text = 9
    Label7.Text = 2
    Label8.Text = 5
    Label9.Text = 2
    Label10.Text = 6

    Dim gradeList As New ArrayList()
    gradeList.Add(Label1.Text)
    gradeList.Add(Label2.Text)
    gradeList.Add(Label3.Text)
    gradeList.Add(Label4.Text)
    gradeList.Add(Label5.Text)
    gradeList.Add(Label6.Text)
    gradeList.Add(Label7.Text)
    gradeList.Add(Label8.Text)
    gradeList.Add(Label9.Text)
    gradeList.Add(Label10.Text)


    'sort grades in an arraylist
    gradeList.Sort()

    ' Please guys help me and modify this code to sort and  SUM UP ONLY best eight done subject grades.
    ' I want label11.text to display the answer 
    'THANKS.

End Sub`

回答by pmcoltrane

As others have suggested, use a strongly-typed collection rather than ArrayList. You probably want grades to be Integerrather than String. It probably won't matter for single-digit grades, but might sort oddly otherwise.

正如其他人所建议的,使用强类型集合而不是ArrayList. 您可能希望成绩是Integer而不是String。对于个位数的成绩,这可能无关紧要,但否则排序可能会很奇怪。

If you change your gradeListvariable into a List(Of Integer)and turn on Option Strict, you'll notice that the label text is String. You can use CIntor Integer.Parseto convert to Integer. They aren't quite the same.

如果您将gradeList变量更改为 aList(Of Integer)并打开Option Strict,您会注意到标签文本是String。您可以使用CIntInteger.Parse转换为Integer. 他们不太一样

You've already sorted the list in your code, so it seems what you're really asking is "How do I sum the first 8 items in a list?"

您已经在代码中对列表进行了排序,因此您真正要问的似乎是“如何对列表中的前 8 个项目求和?

If you think about that question, you should be able to write a For-loop that does so.

如果您考虑这个问题,您应该能够编写一个For-loop 来这样做。

You can also do what you want to do with a one-line LINQ expression:

你也可以用一行 LINQ 表达式做你想做的事情:

Dim sum As Integer = (From grade In gradeList Select grade Order By grade Descending).Take(8).Sum()

回答by pasty

Sorting numbers as strings leads to problems. For example if you add somewhere 10in your grades and sort the array list, like this:

将数字作为字符串排序会导致问题。例如,如果您10在成绩中添加某处并对数组列表进行排序,如下所示:

Dim gradeList As New ArrayList()
gradeList.Add("7")
gradeList.Add("1")
gradeList.Add("3")
gradeList.Add("8")
gradeList.Add("10") '! watch out
' sort string
gradeList.Sort()
' output sorted strings
for each grade in gradeList
    Console.WriteLine(grade)
next grade

the output is:

输出是:

1
10
3
7
8

And it is obviously wrong, because the numbersare compared as literalsand everything starting with 1is less than 3in this case.

它显然是错误的,因为数字进行比较的文字,一切都开始于1小于3在这种情况下。

The problem is easily solved:

问题很容易解决:

  • convert the literals to numbers (or add them directly to the list as integers)
  • put them in a list
  • sort the list (default order is ascending)
  • reverse the sorted list (make the sorting descending)
  • take the first (biggest) and the second element (second biggest) from the list and sum them
  • 将文字转换为数字(或将它们作为整数直接添加到列表中)
  • 把它们放在一个列表中
  • 对列表进行排序(默认顺序是升序)
  • 反转排序列表(使排序降序)
  • 从列表中取出第一个(最大)和第二个(第二大)元素并将它们相加

A solution using a Listand LINQ could look like this:

使用 aList和 LINQ 的解决方案可能如下所示:

' list of integers to be sorted
dim grades as List(of integer) = new List(of integer)
' helper variable holding the parsed grade
dim g as Integer
' convert literals if possible to integers and put them in the list
for each grade in gradeList
    if Integer.TryParse(grade, g) then
        grades.Add(g)
    end if
next grade
' sort the grades as numbers (default is ascending order)
grades.Sort()
' invert the order = descending order
grades.Reverse()
' take the highest two and sum them
dim sum as integer = grades(0) + grades(1)
' or using LINQ ...
'dim sum as integer = grades.Take(1).First() + grades.Skip(1).Take(1).First()
Console.WriteLine(sum)

The output is in this case:

在这种情况下的输出是:

10
8
7
3
1

and the sum is:

总和是:

18