- Home /
Sorting a jagged list based on 2nd entry
Hi, having trouble figuring out a good way to sort my jagged list, each base list contains 6 values. I need to sort the list based on the 2nd value.
Heres an example of what might be in the list
john 2 4 150 ...
sam 4 2 402 ...
kel 1 4 233 ...
ida 3 1 523 ...
I want to sort it so it looks like, the 2nd value being compared.
kel 1 4 233 ...
john 2 4 150 ...
ida 3 1 523 ...
sam 4 2 402 ...
Im kinda lost trying to figure out how to sort a jagged list, if someone can help id be more than greatful. Thanks!
Unity script, but i dont $$anonymous$$d C# or boo as i can read both, ill just rewrite the whole script to whatever language since its only ~60 lines so far
Answer by whydoidoit · Jun 14, 2012 at 06:45 PM
Sorry first answer was a bit of madness!!!
Right well if it was a List of Lists of Object in c#:
using System.Collections.Generic;
using System.Linq;
...
var myBaseList = new List<List<object>>();
var sorted = myBaseList.OrderBy(c=>(int)c[1]).ToList();
Or an array list of array lists:
using System.Linq;
var sorted = myBaseList.OrderBy(c=>(int)((ArrayList)c)[1]).ToArray();
Which gives you an array of array lists.
You can drop the ToArray and ToList if you are just going to do a foreach....
That was fast, Thanks! Looks like a mix of C# and Unity script to me(mainly the var declaration)
Hey ask me about 3D math or shaders and it will take a LOT longer :)
I'm a Linq junkie...
Well those were local variables rather than class level ones... I hate typing syntax twice.
But you are right, if myBaseList was a class level variable - which I bet it will be then yep:
List<List<object>> myBaseList = new List<List<object>>();
The sorted one can stay as var
of course.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Need recommendation for a sorting method for Enemy distances from Player (Ascending) - C# 1 Answer
Sorting a list of GameObjects by accessing their int values 2 Answers
Sorting Game Object Name In Numerical and Alphabetical Order via List 2 Answers
How to alphabetically sort your List? 2 Answers