Reputation: 45096
First and Min() are not the same
Last and Max() are not the same
In the foreach the sort is
aaa
bbb
ccc
^^
Min()
^^
Max()
cc
How can I get Min() and first to be the same?
How can I get Max() and last to be the same?
ss.FirstOrDefault() is not equal ss.Min()
ss.Reverse().FirstOrDefault() is not equal ss.Max()
public void SS()
{
SortedSet<string> ss = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
ss.Add("bbb");
ss.Add("aaa");
ss.Add("^^");
ss.Add("ccc");
foreach (string s in ss)
Debug.WriteLine(s);
Debug.WriteLine(ss.Min());
Debug.WriteLine(ss.Max());
}
Upvotes: 1
Views: 1364
Reputation: 7350
The short answer is: you can't.
The longer answer is in the Min implementation, here.
If you're not comparing numbers, the code will use the Comparer<TSource>.Default
, which is not guaranteed to be the one you are using in the SortedSet
(and in your case isn't).
BTW, if you just want Min and First to be the same, do not specify a comparer when creating the set. It will use the dafault automatically.
Upvotes: 0