I guess I'm too used to use LINQ but this is slow, I did use a profiler and it consume 65% of the time for what I'm trying to do
var unlock = Locked.OrderBy(x => x.Weight) //double
.ThenByDescending(x => x.Stuff?.Level ?? 100) //int
.ThenBy(x => x.Penalty) //double
.FirstOrDefault();
Locked is a list, I know the sort will change the list but I don't really care, I just want to make it work (if possible), the code below doesn't give the same result as the LINQ above;
Locked.Sort(delegate (Node a, Node b)
{
int xdiff = a.Weight.CompareTo(b.Weight);
if (xdiff != 0) return xdiff;
var aStuff = a.Stuff?.Level ?? 100;
var bStuff = b.Stuff?.Level ?? 100;
xdiff = -1 * aStuff.CompareTo(bStuff);
if (xdiff != 0) return xdiff;
return xdiff = a.Penalty.CompareTo(b.Penalty);
});
var unlock = Locked[0];
first thing is, is it possible with the List.Sort to do that complex sorting? asc / then desc / then asc ?
if yes, where is my error?
next is, is there a faster way of doing what I'm trying to do?