简体   繁体   中英

C# List<Tuple<int,int,int>> to sort only by the tuples T1

The list List<Tuple<int,int,int>> .

How do I sort it only by the first component of the tuple T1 from the biggest number to the smallest.

You could try something like this:

var orderedList = list.OrderByDescending(x=>x.Item1);

We use the OrderByDescending method in order we order from the biggest number to the smallest, and passing the lambda x=>x.Item1 , we state the ordering would be done based on the first item of each tuple.

Update

If you want the above statement to be executed immediately and not using deferred execution -when the orderedList will be requested from someone else for 'consumption'-, you should try the following:

 var orderedList = list.OrderByDescending(x=>x.Item1).ToList();

Try using List.Sort with a comparer, in this case Int32.CompareTo

var list = new List<Tuple<int,int,int>>();

list.Sort((x, y) => y.Item1.CompareTo(x.Item1));

Easy with Linq:

var list = new List<Tuple<int,int,int>>();

var ordered = list.OrderBy(t => t.Item1);

排序时可以指定比较器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM