How can I sort a list of 3 variables?
The list has three values: clusCode, DocCode, and xValue.
List<item> unSORT=new list<item>();
var okSORT=from element in unSORT
orderby element.xValue descending
select element;
Your question is very unclear, but I suspect you want something like:
var okSORT = from element in unSORT
orderby element.xValue descending, element.clusCode, element.DocCode
select element;
That's assuming you want it sorted primarily by xValue
(largest first), then by clusCode
(lexicographically earliest first), then by DocCode
(lexicographically earliest first). That will return an IEnumerable<item>
. If you need a List<item>
, you can just use the ToList()
method:
// orderby clause broken into multiple lines to avoid horizontal scrolling -
// that makes no difference
var okSORT = (from element in unSORT
orderby element.xValue descending,
element.clusCode,
element.DocCode
select element).ToList();
At that point it may make sense to use the extension methods directly:
var okSort = unSort.OrderByDescending(element => element.xValue)
.ThenBy(element => element.clusCode)
.ThenBy(element => element.DocCode)
.ToList();
Note that this does not sort the existing list in place. If you need to do that, you should create a Comparer<item>
and use List<T>.Sort
.
I would strongly suggest that you work on your naming, too. The type name, property names and variable names are all unconventional/unclear.
See more on this question at Stackoverflow