Sorting 2 lists based on only one of the lists.

NJDubois

Well-known member
Joined
May 15, 2008
Messages
84
Programming Experience
Beginner
Ok, interesting question that I am sure has to have an easy solution.

I have two lists. One list is a list of names, the other list is a list of how many times each name is found in the first list noted in the database

so...

Nick
John
Jim
Jack

is the firs tlist

10
13
13
2

is the second list. Nick had 10, john 13 and so on.

I want to sort the second list from large to small, but have the index for the first list still linked to the correct amount of calls.

i do it this way so I can

for x = 0 to num_of_names
string = lst_name(x) & "-" & lst_count(x)
next x

There has to be an easy way to do this?


Many thanks for the help guys and gals!

Have a goodone
Nick
 
One way to do what you want is to treat both lists as a key / value pair if you use a generic dictionary collection, as can be seen in the code snippet below.

The name is the key and the number is the value. After creating a dictionary list you could them use LINQ to sort your collection.

VB.NET:
Dim list As New System.Collections.Generic.Dictionary(Of String, Integer)
list.Add("Nick", 10)
list.Add("John", 13)
list.Add("Jim", 13)
list.Add("Jack", 2)
 
Dim sortedList = From item As KeyValuePair(Of String, Integer) In list
Order By item.Value
 
Return sortedList.ToList

The useage of this code snippet is dependant on what version of vb you are using vb 2010, and 2008 support generic collections and LINQ.

Hope this helps.

Regards

Scott
 
Back
Top