x


How do you use LINQ to alphabetize a List of Unity Objects by name?

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.)

more ▼

asked Mar 08 '11 at 07:30 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You'd do something like:

var ordered = yourList.OrderBy(x=>x.name);

Or as you've mentioned:

yourList = yourList.OrderBy(x=>x.name).ToList();

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:

yourList.Sort((x,y) => x.name.CompareTo(y.name));
more ▼

answered Mar 08 '11 at 07:39 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

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)
10|3000 characters needed characters left

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.

more ▼

answered Mar 10 '12 at 12:54 AM

InsidiousForce gravatar image

InsidiousForce
0

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 10 '12 at 12:54 AM

InsidiousForce gravatar image

InsidiousForce
0

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x27

asked: Mar 08 '11 at 07:30 PM

Seen: 2133 times

Last Updated: Mar 10 '12 at 12:54 AM