I've got a list defined like this...
var sets = new List<HashSet<int>>(numSets);
Why isn't there an overload so I can sort it like this?
sets.Sort(s => s.Count);
I want the largest set first. What's the easiest way to do that?
I've got a list defined like this...
var sets = new List<HashSet<int>>(numSets);
Why isn't there an overload so I can sort it like this?
sets.Sort(s => s.Count);
I want the largest set first. What's the easiest way to do that?
Because List<T>
class was introduced in .NET 2.0 and the designers of this class decided so. You could use the OrderByDescending
extension method:
sets = sets.OrderByDescending(s => s.Count).ToList();
Try this:
sets.Sort((setA, setB) => setB.Count.CompareTo(setA.Count));
This uses the Sort(Comparison<T> comparison)
overload of List<T>.Sort
.
The fact that the expression compares B with A rather than A with B is what produces the descending-by-count order that you require.
The reason your code doesn't work is because List<T>.Sort
, unlike Enumerable.OrderByDescending
, does not have an overload that accepts a Func<TSource, TKey>
key-selector.
@Darin Dimitrov's technique of using OrderByDescending
is fine too, but note that this will create a sorted list out of place and reassign the reference you have to the original list to the new, sorted one.
Little confused...
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; ++i)
sets.Sort((s1, s2) => s2.Count.CompareTo(s1.Count));
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Start();
for (int i = 0; i < 1000; ++i)
sets = sets.OrderByDescending(s => s.Count).ToList();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Gives me "1499, 2107", but if I switch the order, I get "589, 2023". Why is there such a big difference?