简体   繁体   中英

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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