vb.net 在 VB 中对数组进行排序而不使用 Sort 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13861249/
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
Sorting array in VB without using Sort Function
提问by Ds.109
I'm working on a sorting program where it takes inputs into an array. I already made Min, Max, and Average. Now I need to do Median, Mode and Sorting (Max to Min, and Min to Max).
我正在开发一个排序程序,它将输入输入到一个数组中。我已经制作了最小值、最大值和平均值。现在我需要做中位数、模式和排序(最大到最小,最小到最大)。
Here's the code I got for sorting [UPDATED New Code]
这是我得到的用于排序的代码 [更新的新代码]
RichTextBox1.Text = RichTextBox1.Text.Replace(" ", ",")
marks = RichTextBox1.Text.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
Label3.Text = Nothing
Dim z As Integer = marks.Length - 1
Dim y As Integer
Dim TEMP As Integer
For X = 1 To z
For y = 1 To (z - 1)
If marks(y) > marks(y + 1) Then
TEMP = marks(y)
marks(y) = marks(y + 1)
marks(y + 1) = TEMP
End If
Label3.Text = Label3.Text & vbCrLf & marks(y)
Next y
Next X
采纳答案by Teejay
This is the Selection Sort algorithm from Wikipediaconverted to VB.net
这是从维基百科转换为 VB.net的选择排序算法
Public Shared Function Sort(ByVal a As Int32()) As Int32()
Dim n As Int32 = a.Length ' a(n-1) is the last element
Dim i As Integer, j As Integer
Dim iMin As Integer
' Advance the position through the entire array
' we could do "from j = 0 to n-2" (that is "for j < n-1")
' because single element is also min element
For j = 0 To n - 2
' Find the min element in the unsorted a[j .. n-1]
' Assume the min is the first element
iMin = j
' Test against elements after j to find the smallest
For i = j + 1 To n - 1
' If this element is less, then it is the new minimum
If a(i) < a(iMin) Then
' Found new minimum, remember its index
iMin = i
End If
Next
' iMin is the index of the minimum element,
' swap it with the current position
If iMin <> j Then
Dim tmp As Int32 = a(j)
a(j) = a(iMin)
a(iMin) = tmp
End If
Next
Return a
End Function
Reversing it to do a descending sort should not be difficult, once you've fully understood the above.
一旦您完全理解了上述内容,反转它以进行降序排序应该不难。
回答by Neolisk
If you don't like Sort, you can use OrderBy. LINQ also allows you to calculate Min, Maxand Averagedirectly, so you don't need to reinvent the wheel. See this article for more information:
如果你不喜欢Sort,你可以使用OrderBy。LINQ还可以计算Min,Max而Average直接,所以你不需要推倒重来。有关更多信息,请参阅这篇文章:
Using LINQ to Calculate Basic Statistics
(includes code for Variance, StandardDeviation, Median, Mode etc.)
(包括方差、标准偏差、中值、模式等的代码)
Note: The code is C#, but it can be easily converted to VB.NET, since both are .NET languages.
注意:代码是 C#,但它可以很容易地转换为 VB.NET,因为两者都是 .NET 语言。
回答by user3469294
To sort a multidimensional array, you can use exactly the same procedure as for sorting a single dimension array.
要对多维数组进行排序,可以使用与对单维数组进行排序完全相同的过程。
You loop through the array, and if two adjacent members are in the wrong order, then you swop them around.
您遍历数组,如果两个相邻成员的顺序错误,则将它们交换。
Imagine you have a multi-dimension array such as
想象一下你有一个多维数组,比如
Dim Array(12, 4) String
So, suppose you want to order the array by the last column for example.
因此,假设您想按最后一列对数组进行排序。
For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If Array(index2, 4) > Array(index2 + 1, 4) Then
// this sorts the last column
Temp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4)
Array(index2 + 1, 4) = Temp4
// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same way
Temp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3)
Array(index2 + 1, 3) = Temp3
Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2)
Array(index2 + 1, 2) = Temp2
Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1)
Array(index2 + 1, 1) = Temp1
Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0)
Array(index2 + 1, 0) = Temp0
Next
Next
So now all the columns are sorted by the last column.
所以现在所有的列都按最后一列排序。
You might be wondering how you can sort if 4 columns are strings and one column is a number, and suppose you want to sort by the number column.
您可能想知道如果 4 列是字符串并且一列是数字,并且假设您想按数字列进行排序,如何进行排序。
You could do this
你可以这样做
For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If CSng(Array(index2, 4)) > CSng(Array(index2 + 1, 4)) Then
// this sorts the last column
Temp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4)
Array(index2 + 1, 4) = Temp4
// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same way
Temp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3)
Array(index2 + 1, 3) = Temp3
Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2)
Array(index2 + 1, 2) = Temp2
Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1)
Array(index2 + 1, 1) = Temp1
Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0)
Array(index2 + 1, 0) = Temp0
Next
Next
All I have done is to convert the Datatype to the last column from String to Single, then compare the adjacent values as before.
我所做的只是将 Datatype 转换为最后一列从 String 到 Single,然后像以前一样比较相邻的值。
So this is a very basic way of sorting a multidimensional array, and it works for mixed arrays containing strings and numbers as well.
所以这是对多维数组进行排序的一种非常基本的方法,它也适用于包含字符串和数字的混合数组。

