List<KeyValuePair<TKey, TValue>> সর্ট করতে Sort() মেথডে কাস্টম কম্পারিজন লজিক দেওয়া যায়। নিচে ভ্যালু অনুযায়ী ডাউনসর্টিং দেখানো হয়েছে।
উদাহরণ
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var list = new List>();
list.Add(new KeyValuePair(1, 20));
list.Add(new KeyValuePair(2, 15));
list.Add(new KeyValuePair(3, 35));
list.Add(new KeyValuePair(4, 50));
list.Add(new KeyValuePair(5, 25));
Console.WriteLine("Unsorted:");
foreach (var v in list) Console.WriteLine(v);
// ভ্যালু অনুযায়ী ডাউনসর্ট
list.Sort((x, y) => y.Value.CompareTo(x.Value));
Console.WriteLine("Sorted (Descending by Value):");
foreach (var v in list) Console.WriteLine(v);
}
}
আউটপুট
Unsorted... [1, 20] [2, 15] [3, 35] [4, 50] [5, 25] Sorted (Descending by Value)... [4, 50] [3, 35] [5, 25] [1, 20] [2, 15]