You can use OrderByDescending + ThenBy
(assuming that it's aList<int?>
):
var orderedList = ListA
.OrderByDescending(x => x.HasValue)
.ThenBy(x => x);
x.HasValue
returns true
or false
where true
is higher than false
. That's why i'm using OrderByDescending
.
If you want to sort the original list i would use List.Sort
with a custom Compaison<T>
that treats null
as highest value:
ListA.Sort((a1, a2) => (a1 ?? int.MaxValue).CompareTo(a2 ?? int.MaxValue));
This is more efficient since it doesn't need to create a new list.