How would I go about 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.
我将如何使用元组的第一个元素作为确定顺序的值,按降序排序,使用List
4 个解决方案
#1
39
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
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
.
编辑:我现在修改了上面的内容,按降序排序。请注意,正确的方法是颠倒比较的顺序(y到x而不是x到y)。你不能只是否定CompareTo的返回值 - 当CompareTo返回int.MinValue时,这将失败。
#2
9
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?
它是就地的,不是吗?
#3
4
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
list.Sort((x,y)=> x.Item1.CompareTo(y.Item1));
#4
3
var listSort = from element in list orderby element.Item1 element.Item2 select element;
#1
39
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
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
.
编辑:我现在修改了上面的内容,按降序排序。请注意,正确的方法是颠倒比较的顺序(y到x而不是x到y)。你不能只是否定CompareTo的返回值 - 当CompareTo返回int.MinValue时,这将失败。
#2
9
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?
它是就地的,不是吗?
#3
4
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
list.Sort((x,y)=> x.Item1.CompareTo(y.Item1));
#4
3
var listSort = from element in list orderby element.Item1 element.Item2 select element;