开发者

Sort List<Tuple<int, int>> in-place

How would I go abou开发者_高级运维t sorting in descending order, a List<Tuple<int, int>> using the first element of the tuple as the value that determines the order? It has to be in-place and I only know how to do it using LINQ which returns a new list.


You just need to provide an IComparer<Tuple<int, int>> or a Comparison<Tuple<int, int>> to the List<T>.Sort method. The latter is probably easier to specify inline:

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

If you want to order by the first value and then the second value, it becomes a bit trickier, but still feasible. For example:

list.Sort((x, y) => {
    int result = y.Item1.CompareTo(x.Item1);
    return result == 0 ? y.Item2.CompareTo(x.Item2) : result;
});

EDIT: I've now amended the above to sort in descending order. Note that the right way to do this is to reverse the order of the comparison (y to x instead of x to y). You must not just negate the return value of CompareTo - this will fail when CompareTo returns int.MinValue.


Why not this?

List<Tuple<int, int>> list = ...
list = list.OrderBy(i => i.Item1).ToList();

Yes, it creates a new list, but I'm just interested - why don't you like this?


List<Tuple<int, int>> list = new List<Tuple<int, int>>
{
    new Tuple<int,int>(1,1),
    new Tuple<int,int>(0,2),
    new Tuple<int,int>(3,0)
};

list.Sort(Comparer<Tuple<int, int>>.Default);

produces:

0,2
1,1
3,0

And it's in-place, isn't it?


Now you can use lambda to do it:

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


Have you looked at the List<T>.Sort method? You can use an overload that takes a Comparison<T> delegate or an IComparer<T>:

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


var listSort = from element in list orderby element.Item1  element.Item2 select element;


I Needed to sort a List of IntPoints on the X values. I was able to do this by modifying a previous example. I am going to put my example here in case anyone needs this example in the future.

//Where edges is an unsorted list of edgepoints returned from the C# A.Forge Library
List<IntPoint> edges

//Sorting on the X value of the Intpoint and returning a new List
List<IntPoint> edgesSorted = (from point in edges orderby point.X select point).ToList();


Here is an example with the modern tuple type, which was added in C# 7.

using System;
using System.Collections.Generic;

namespace SortTuples
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new List<(string Name, int Grade)>() 
            {
                ("Patrick", 89),
                ("Lucia", 92),
                ("Veronika", 72),
                ("Robert", 78),
                ("Maria", 65),
                ("Andrea", 51),
                ("Ondrej", 45)
            };

            data.Sort((s1, s2) => s1.Name.CompareTo(s2.Name));
            Console.WriteLine(string.Join(", ", data));
  
            data.Sort((s1, s2) => s2.Grade.CompareTo(s1.Grade));
            Console.WriteLine(string.Join(", ", data));
        }
    }
}

The output:

$ dotnet run
(Andrea, 51), (Lucia, 92), (Maria, 65), (Ondrej, 45), (Patrick, 89), (Robert, 78), (Veronika, 72)
(Lucia, 92), (Patrick, 89), (Robert, 78), (Veronika, 72), (Maria, 65), (Andrea, 51), (Ondrej, 45)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜