|
I've never used LINQ, but it looks like the way to go, for this operation. How do you do it? (If you know of a good beginner's tutorial on LINQ, that looks like something that could be used with Unity, please let me know.)
(comments are locked)
|
|
You'd do something like:
Or as you've mentioned:
where yourList is IEnumerable, eg, List< Transform> or GameObject[] http://msdn.microsoft.com/en-us/vcsharp/aa336746 http://www.dotnetperls.com/linq Edit: good old List.Sort for an inline sort: Can you not just order it in place? I came up with xs = xs.OrderBy(x => x.name).ToList();
Mar 08 '11 at 07:41 PM
Jessy
Yeah, you'd have to do that if you wanted to do it in place, it's designed to work with all types of data, most of which doesn't even exist in a collection. I'll change my example to yours though, since you want the object, not the name in the final list :D
Mar 08 '11 at 07:43 PM
Mike 3
What I mean is, I want to do something that doesn't require an assignment. I want the list sorted; I don't need a copy of it.
Mar 08 '11 at 07:55 PM
Jessy
Indeed - let me make an example with an inline sort, one sec
Mar 08 '11 at 07:59 PM
Mike 3
Hmm, lambda support was added in as one of the backend items needed to facilitate linq (it'd be horrendous to write without), so it's at least made easier by the fact linq was created ;)But no, List.Sort has been there since List was added. Anyway - if you're doing a simple sort, Linq is going to give you some overheads that you don't want, but if you try sorting by multiple items, List.Sort quickly becomes unusable as it's not a stable sort. At that point, you'd want to be using Linq's OrderBy/OrderByDescending and ThenBy/ThenByDescending to sort.
Mar 10 '11 at 01:09 PM
Mike 3
(comments are locked)
|
|
The Q in LINQ stands for Query. It's designed as a way to query data in-language. Querying for all objects with x = 1 and ordered by y, that's perfect. It returns an enumeration of object references, which you can query further, convert to an array, etc. But it will not modify the original data.
(comments are locked)
|
|
The Q in LINQ stands for Query. It's designed as a way to query data in-language. Querying for all objects with x = 1 and ordered by y, that's perfect. It returns an enumeration of object references, which you can query further, convert to an array, etc. But it will not modify the original data.
(comments are locked)
|
