[英]How can i Sort a List<Tuple<int, double>>
您好,感謝您閱讀此帖。
我有一個以這種方式創建的列表
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
Ratings.OrderByDescending(t => t.Item2);
你可以簡單地使用
Ratings = Ratings.OrderByDescending (t => t.Item2).ToList();
您是否嘗試過使用列表中的Sort
方法,intellisense應該向您建議它,它有點自然:
Ratings.Sort((x, y) => y.Item2.CompareTo(x.Item2));
// at this stage the Ratings list will be sorted as desired
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();
var comparer = Comparer<Tuple<int, double>>.Create((x, y) => -1 * x.Item2.CompareTo(y.Item2));
Ratings.Sort(comparer);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.