简体   繁体   English

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

[英]Sorting an array numerically (VB.NET)

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. 一如既往,我感谢您的所有帮助。

You can split the data into two arrays and use use array.sort to sort based on the integers. 您可以将数据拆分为两个数组,并使用use array.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 . 这将以ia升序对两个数组进行排序。 Iterate the arrays backward to get descending order. 向后迭代数组以获得降序。

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

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

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