Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Program
- {
- public static void Main(string[] args)
- {
- List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
- list.Add(new KeyValuePair<int, string>(1, "A"));
- list.Add(new KeyValuePair<int, string>(3, "F"));
- list.Add(new KeyValuePair<int, string>(4, "G"));
- list.Add(new KeyValuePair<int, string>(2, "B"));
- list.Add(new KeyValuePair<int, string>(2, "C"));
- list.Add(new KeyValuePair<int, string>(3, "E"));
- list.Add(new KeyValuePair<int, string>(3, "D"));
- list=list.OrderBy(a=>a.Key).ToList();
- foreach(var item in list)
- {
- Console.WriteLine(item);
- }
- }
- }
- [1, A]
- [2, B]
- [2, C]
- [3, F]
- [3, E]
- [3, D]
- [4, G]
- list=list.OrderBy(a => a.Key).ThenBy(a => a.Value).ToList();
- list = list
- .GroupBy(kvp => kvp.Key)
- .OrderBy(kvp => kvp.Key)
- .SelectMany(g => g.Reverse())
- .ToList();
- list
- .Zip(Enumerable.Range(0, list.Count), (item, insertOrder) => new { Item = item, InsertOrder = insertOrder })
- .OrderBy(x => x.Item.Key)
- .ThenBy(x => x.InsertOrder)
- .Select(x => x.Item)
- .ToList();
Add Comment
Please, Sign In to add comment