Sorting an array numerically (VB.NET)

2019-08-30 02:11发布

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.

As always I appreciate all of your help.

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-30 02:39
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
查看更多
Explosion°爆炸
3楼-- · 2019-08-30 02:45

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

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-08-30 02:45
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
查看更多
登录 后发表回答