0

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.

2
  • "name1" is not integer and has no integers in it - the 1 is a numeral and has no more value than "n" or "a". You likely need to split the data into the text portion and numeric portion for the sorting. If you parse it into a Class of Name/Value you perhaps could make use of a SortedList Commented Apr 15, 2014 at 19:11
  • How are you currently keeping this data in memory? Commented Apr 15, 2014 at 21:06

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

0
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.