- Home /
Orderby/Sort a List of Lists based on the .Count
I have a list that countains lists of the type actorKnowledge
var scriptKnowledgeList = new List.<List.<ActorKnowledge> >();
What I would like to do is to sort the scriptKnowledgeList
based on the size of each one of its items.
Lets say scriptKnowledgeList
contains 3 elements that were added in the following order:
ListA.Count = 1;
ListB.Count = 5;
ListC.Count = 2;
I would like them to be re-ordered inside scriptKnowledgeList
from bigger to smaller:
ListB.Count = 5;
ListC.Count = 2;
ListA.Count = 1;
How would I go about this? Thanks in advance!
UPDATE:
Ok, I think I got it, wondering if there is a better or more correct way of doing it?
scriptKnowledgeList = scriptKnowledgeList.OrderBy(function(x){return -x.Count;}).ToList();
Answer by Eric5h5 · Dec 28, 2013 at 09:22 PM
scriptKnowledgeList.Sort (function (a : List.<ActorKnowledge>, b : List.<ActorKnowledge>) b.Count.CompareTo(a.Count));
Thanks for the quick reply. Which one is more correct/faster? The Sort or the OrderBy? Do they create the same end result?
The OrderBy is creating a new list so I assume the Sort is a little more efficient in terms of not generating garbage. I doubt there would be any noticeable performance difference either way.
Your answer
Follow this Question
Related Questions
List.Sort with IComparer 2 Answers
sort a List of GameObjects by distance? 1 Answer
Get a list of children (in order) 1 Answer
A node in a childnode? 1 Answer
How to arrange list 3 Answers