0

I have two lists, one integer and one string. These values are entered during a loop, so they are associated together (e.g. ListOfInteger.Item(i) and ListOfString.Item(i) were entered at the same time and are related to each other). I sorted and subsequently reversed the list of integers. Is there any way to have the list of strings still associated with the list of integers, in order to display them in a text box. For example:

    List of Strings (surname):         List of Integers (score):
            Jones                                 4
            Perry                                 2
            Morris                                6

    List of Strings (surname):        Sorted List of Integers: 
            Jones                              2
            Perry                              4
            Morris                             6  

Edit:

If (name.Count + 1) < compnum Then
        name.Add(txtName.Text)
        score.Add(txtScore.Text)
Else 
        txtName.Text(Hide)
        txtScore.Text(Hide)
        btnSort.Text(Show)
End If  

...

score.Sort()
score.Reverse()
txtSortedScore1.Text = score(0) 
(and so forth)

How can I relate these two lists together in order to associate the data in the string list with the sorted list of integers?

Edit - The end result should look like this:

 List of Strings (surname):        Sorted List of Integers: 
            Perry                              2
            Jones                              4
            Morris                             6
  • I think a [**`Dictionary(Of TKey, TValue)`**](https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx) could be useful here. Or even better a [**`SortedDictionary(Of TKey, TValue)`**](https://msdn.microsoft.com/en-us/library/f7fta44c(v=vs.110).aspx). – Visual Vincent Aug 24 '16 at 14:10
  • Rather than storing **closely related** data in parallel collections, use a class: [Five Minute Guide to Classes and Lists](http://stackoverflow.com/a/34164458/1070452) – Ňɏssa Pøngjǣrdenlarp Aug 24 '16 at 15:59

3 Answers3

1

The techniques suggested by the others to use a container class or dictionary are better solutions. However to answer the question as stated, what you seeking to do is perform a keyed sort. Unfortunately, the List(Of T) class does not provide this functionality; it is provided by the Array Class.

This is a bit convoluted as first you dump the two lists to arrays, sort the arrays, and finally recreate the lists with the sorted results.

Dim keysList As List(Of Int32) = New List(Of Integer)(New Int32() {4, 2, 6})
Dim itemsList As List(Of String) = New List(Of String)(New String() {"Jones", "Perry", "Morris"})

Dim keys As Int32() = keysList.ToArray
Dim items As String() = itemsList.ToArray
Array.Sort(keys:=keys, items:=items)

keysList = New List(Of Integer)(keys)
itemsList = New List(Of String)(items)
TnTinMn
  • 11,522
  • 3
  • 18
  • 39
0

You should wrap your string and integer in another object.

Class Person
    Public String SurName
    Public Int Score
End Class

From there you can manipulate your objects in any way you like and then iterate over them and output the data as you like.

Something like this:

Dim persons = New List(Of Person)()

persons.Add(New Person() With { _
    Key .SurName = "Jones", _
    Key .Score = 4 _
})

For Each p As var In persons.OrderBy(Function(x) x.Score)
    ListBox.Add(p.SurName + p.Score)
Next

If you would provide your code, we could help you more.

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
0

There are few ways the names can be sorted. Here is one way:

Dim names = {"Jones", "Perry", "Morris"}, score = {4, 2, 6}

Dim sortedIndexes = Enumerable.Range(0, score.Length).OrderBy(Function(i) score(i)).ToArray ' { 1, 0, 2 }

Dim sortedNames = sortedIndexes.Select(Function(i) names(i)).ToList ' { "Perry", "Jones", "Morris" }
Dim sortedScore = sortedIndexes.Select(Function(i) score(i)).ToList ' { 2, 4, 6 }
Slai
  • 22,144
  • 5
  • 45
  • 53