Order a List (C#) by many fields?

Posted by Esabe on Stack Overflow See other posts from Stack Overflow or by Esabe
Published on 2010-05-06T07:51:52Z Indexed on 2010/05/06 7:58 UTC
Read the original article Hit count: 142

Filed under:
|
|

Hi everyone,

I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order:

object1 => A = "a", B = "h"
object2 => A = "a", B = "c"
object3 => A = "b", B = "x"
object4 => A = "b", B = "b"

and I want to order the list by A attribute first, and when they are equals, by B element, so the order would be:

"a" "c"
"a" "h"
"b" "b"
"b" "x"

As far as I know, the OrderBy method order by one parameter.

Question: How can I order a C# List by more than one field?

Thank you very much

© Stack Overflow or respective owner

Related posts about orderby

Related posts about c#