我如何排序列表<tuple <int,double =“”>&gt; </tuple <int,>

时间:2015-03-02 12:29:35

标签: c# list sorting

您好,感谢您阅读此帖。

我有一个以这种方式创建的列表

List<Tuple<int, double>> Ratings = new List<Tuple<int, double>>();

让我们说清单的价值如下

Index      int     double

[0]        1       4,5
[1]        4       1,0
[2]        3       5,0
[3]        2       2,5

如何对列表进行排序,以便最高的双值?像这样

Index      int     double
[0]        3       5,0
[1]        1       4,5
[2]        2       2,5
[3]        4       1,0

5 个答案:

答案 0 :(得分:9)

Ratings.OrderByDescending(t => t.Item2);

答案 1 :(得分:5)

您只需使用

即可
Ratings = Ratings.OrderByDescending (t => t.Item2).ToList();

答案 2 :(得分:3)

你是否尝试过使用列表中的Sort方法,intellisense应该向你建议它,它有点自然:

Ratings.Sort((x, y) => y.Item2.CompareTo(x.Item2));
// at this stage the Ratings list will be sorted as desired

答案 3 :(得分:3)

List<Tuple<int, double>> Ratings = new List<Tuple<int, double>>();

                    Ratings.Add(new Tuple<int, double>(1, 4.5));
                    Ratings.Add(new Tuple<int, double>(4, 1.0));
                    Ratings.Add(new Tuple<int, double>(3, 5.0));
                    Ratings.Add(new Tuple<int, double>(2, 2.5));

                    var list = Ratings.OrderByDescending(c => c.Item2).ToList();

答案 4 :(得分:0)

var comparer = Comparer<Tuple<int, double>>.Create((x, y) => -1 * x.Item2.CompareTo(y.Item2));
Ratings.Sort(comparer);
相关问题