vb.net 对数组(或列表)进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14969443/
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 12:24:27  来源:igfitidea点击:

Sorting an array (or list)

arraysvb.netsorting

提问by Mohammad Tomaraei

I have a loop and in this loop every time I get 3 different values, x, r, and c.

我有一个循环,在这个循环中每次我得到 3 个不同的值,x、r 和 c。

I want an array (I have tried using a list but I don't know how to sort it, searched a lot!) which will input the all the 3 values at each round of the loop in a new automatic index of the array, and at the end I want to sort this whole array based on ascending values (x, then r, and then c).

我想要一个数组(我尝试过使用列表,但我不知道如何对其进行排序,搜索了很多!)它将在循环的每一轮中输入所有 3 个值,并将其输入到数组的新自动索引中,最后,我想根据升序值(x,然后是 r,然后是 c)对整个数组进行排序。

For example these are my values in an array getting added (I don't need a value for this array, these values (1,2,3) are the values, something like a table)

例如,这些是我在数组中添加的值(我不需要该数组的值,这些值 (1,2,3) 是值,类似于表格)

storage(1,2,3)
storage(2,3,4)
storage(3,4,5)
storage(4,5,6)

These 3 are actually co-ordinates of positions of a container in different stacks, and I want to find out which one is closer to the surface, and in a closer stack. Therefore I'm adding them into an array, and then I will sort them so I can find which one is closer (provided that all of these are matches for the same type of container, located at different places)

这3个其实是一个容器在不同栈中的位置坐标,我想找出哪个更靠近表面,在更近的栈中。因此,我将它们添加到一个数组中,然后对它们进行排序,以便我可以找到哪个更接近(前提是所有这些都是位于不同位置的同一类型容器的匹配项)

回答by SysDragon

Simply use the .Sort()method. It will use the IComparableinterface implemented by each element of the Array. If you dont specify a custom comparer it will use the default, which is ok for Integervalues.

只需使用该.Sort()方法。它将使用IComparableArray. 如果您不指定自定义比较器,它将使用默认值,这对于Integer值来说是可以的。

Array:

大批:

Dim iNums() As Integer = New Integer() {1, 11, 7, 5}
Array.Sort(iNums)

List:

列表:

Dim aux As New List(Of Integer)(New Integer() {1, 11, 7, 5})
aux.Sort()

And if you need you can define you own comparer:
http://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.71).aspx

如果您需要,您可以定义自己的比较器:http: //msdn.microsoft.com/en-us/library/system.icomparable(v=vs.71)
.aspx