开发者

C# Sorting a list by X and Y

I need to sort a list, by X and Y. I read some on other questions to sort X and Y, but I didn't understand the:

List<SomeClass>() a;
List<SomeClass> b = a.OrderBy(x =>开发者_开发知识库; x.x).ThenBy(x => x.y).ToList();

I mean, x => x.x gives undeclared variable error.

Question is: How to sort by X and Y in a list, or: What variable to have instead of x => x.x????

edit: right, my list is like this:

List<Class1> MapTiles;

and in class: int ID, int X, int Y

Thanx.


C# is case sensitive. You need

// Note the capital letters
List<SomeClass> b = a.OrderBy(item => item.X).ThenBy(item => item.Y).ToList();


dlev's answer is the correct one to this question, the following is just an extended explanation of a concept that Marcus may have not understood fully

Just to clarify some point that was brought up in the comments, what's happening is you're actually passing in a delegate to do the sorting.

The following snippet

List<Item> b = a.OrderBy(item => item.X);

Is like creating a static compare function that sorts by comparing the field (or property) X of an object of type Item and passing that compare function into a sort function, as you might do in C++.

The OrderBy(...) is just a very short and convenient way of accomplishing this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜