[Solved] Cannot group strings correctly if use IEnumerable.GroupBy in C# [closed]


To really make sure it comes out as you want it, you could do it like that:

var val = val1
    .GroupBy(f => Path.GetFileNameWithoutExtension(f)) //group by filename
    .OrderBy(group => group.Key); //here's the "Sort"

foreach (var group in val)
{
    var q = group.OrderByDescending(f => Path.GetExtension(f)); //order the filenames for outputting
    foreach (var f in q)
    {
        Console.WriteLine(f);
    }
}

I’d reconsider your task though and perhaps make it clear why you want to exactly that

Update: in case you really need the grouping. Without the grouping you may find nice answers below that are much shorter.

4

solved Cannot group strings correctly if use IEnumerable.GroupBy in C# [closed]