Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort list of list by multiple inner list values.

Status
Not open for further replies.

IknowMe

Programmer
Aug 6, 2004
1,214
US
Hopefully I explained my need well enough in the Thread title. If not I will elaborate further.

I have a list of array that I am populating with a list of string. I need to sort the list of array (outer) by two items in the list of string (inner).

The real data i'll be using has any number of batches which each include 25 items indexed by sequence number. The data comes randomly to me as a list of string which i populate to a list of array. I'd really like to be able to sort it by batch and sequence within the batch.

So i end up with something like this

batch 1, seq 1
batch 1, seq 2

batch 2, seq 1
batch 2, seq 2

ect.

This is probably quite easy for some however I am just learning vb.net. I am using visual studio 2008 if it helps.

I have the following code thus far to build a pretend list of list and am trying to play with the sorting on my own now. Hopefully with your help this is a quick and painless learning experience for me and a star for you.

I've done some googling but am not really sure what a good search term for this example would be, even that would help me alot.

Code:
Dim LineArray As New List(Of Array)
    Dim Lines As New List(Of String)
    Dim batch As Integer
    Dim Sequence As Integer
 
    For batch = 2 To 1 Step -1
      For Sequence = 5 To 1 Step -1
        Lines.Add(Str(batch))
        Lines.Add(Str(Sequence))
        LineArray.Add(Lines.ToArray)
        Lines.Clear()
      Next
    Next



[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
After some poking around I came up with the following.

Code:
    Dim Lines As New List(Of String)
    Dim batch As Integer
    Dim Sequence As Integer
    Dim mySortedList As New SortedList(Of Integer, Array)
 
    For batch = 2 To 1 Step -1
      For Sequence = 5 To 1 Step -1
        Lines.Add(Str(batch))
        Lines.Add(Str(Sequence))
        mySortedList.Add(cstr(lines(0)) & cstr(lines(1)), Lines.ToArray)
        Lines.Clear()
      Next
    Next

Which works okay for my imediate need. I am still curious though. Had my item and batch been non-numeric creating a sortable key would be tougher. So if anyone has an example of sorting objects by multiple attributes I'd love to see it.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top