以数字方式对数组进行排序 (VB.NET)

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

Sorting an array numerically (VB.NET)

arraysvb.netsortingcsv

提问by SkyTrees

I want to sort records based on their integer value in descending order:

我想根据整数值按降序对记录进行排序:

Example:

例子:

name1, 4
name2, 6
name3, 3
name4, 5

Should be re - arranged to this:

应该重新安排成这样:

name2, 6
name4, 5
name1, 4
name3, 3

I've tried using the Array.Sort but I could not get it working.

我试过使用 Array.Sort 但我无法让它工作。

As always I appreciate all of your help.

一如既往,我感谢您的所有帮助。

回答by xpda

You can split the data into two arrays and use use array.sortto sort based on the integers.

您可以将数据拆分为两个数组,并使用 usearray.sort根据整数进行排序。

Dim a() As String = {"name1", "name2", "name3", "name4"}
Dim ia() As Integer = {4, 6, 3, 5}
Array.Sort(ia, a)

This will sort both arrays in ascending order of ia. Iterate the arrays backward to get descending order.

这将按 的升序对两个数组进行排序ia。向后迭代数组以获得降序。

回答by user98937

Sub Main()
    Dim StartArray(3) As Integer
'First let's assign the array elements before it is sorted
        StartArray(0) = 4
        StartArray(1) = 6
        StartArray(2) = 3
        StartArray(3) = 5
        Array.Sort(StartArray) 'This sorts the array
        For i As Integer = 0 To 3
            Console.WriteLine(StartArray(i)) 'Prints the array elements to console
        Next
        Console.ReadLine()
End Sub

回答by Fortune Masiiwa

dim nos() as integer={1,2,3,4}
dim names() as string = {"a","b","c","d"}
for i = 0 to 3
     array.sort(names &"  "&nos)
next
console.readKey