Sort and Group in LINQ
Posted
by Jono
on Stack Overflow
See other posts from Stack Overflow
or by Jono
Published on 2010-04-27T19:07:33Z
Indexed on
2010/04/27
19:13 UTC
Read the original article
Hit count: 303
I apologise in advance if this has been answered before; it seems so fundamental to me yet I haven't turned up the answer in any of my searches.
I have a list of string tuples, say (P1,P2)
I'd like to know if there's a LINQ statement where I could group by P1 (in ascending order), and have that group contain all the P2 values for the group (in descending order).
For input: ("A","B"), ("A","C"), ("D","B") I'd like to get two groups: "A" and "D" (in that order, every time) where group "A" contains "C" and "B" (in that order, every time) and group "D" contains, well, "B".
Is this possible with the built-in LINQ classes or do I need to iterate the groups and sort them myself? Sounds like hard work~
© Stack Overflow or respective owner