I assume you want to order both groups and words within a group? Try the following:
var groups = from w in words
group w by w[0] into g
select new { FirstLetter = g.Key, Words = g.OrderBy(x => x) };
var wordList = groups.OrderBy(x => x.FirstLetter).ToList();
or
var groups = from w in words
group w by w[0] into g
orderby g.Key
select new { FirstLetter = g.Key, Words = g.OrderBy(x => x) };
var wordList = groups.ToList();
(The second form is what I originally had in my answer, except that I included a space in the "orderby" that caused the compilation to fail. I wonder why that was? Doh!)
Of course, you could do all this in a single point statement:
var wordList = words.GroupBy(word => word[0])
.OrderBy(group => group.Key)
.Select(group => new { FirstLetter = group.Key,
Words = group.OrderBy(x => x) })
.ToList();
source
share