Guest User

Untitled

a guest
May 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class Program
  2. {
  3. public static void Main(string[] args)
  4. {
  5. List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
  6.  
  7. list.Add(new KeyValuePair<int, string>(1, "A"));
  8. list.Add(new KeyValuePair<int, string>(3, "F"));
  9. list.Add(new KeyValuePair<int, string>(4, "G"));
  10. list.Add(new KeyValuePair<int, string>(2, "B"));
  11. list.Add(new KeyValuePair<int, string>(2, "C"));
  12. list.Add(new KeyValuePair<int, string>(3, "E"));
  13. list.Add(new KeyValuePair<int, string>(3, "D"));
  14.  
  15. list=list.OrderBy(a=>a.Key).ToList();
  16.  
  17. foreach(var item in list)
  18. {
  19. Console.WriteLine(item);
  20. }
  21. }
  22. }
  23.  
  24. [1, A]
  25. [2, B]
  26. [2, C]
  27. [3, F]
  28. [3, E]
  29. [3, D]
  30. [4, G]
  31.  
  32. list=list.OrderBy(a => a.Key).ThenBy(a => a.Value).ToList();
  33.  
  34. list = list
  35. .GroupBy(kvp => kvp.Key)
  36. .OrderBy(kvp => kvp.Key)
  37. .SelectMany(g => g.Reverse())
  38. .ToList();
  39.  
  40. list
  41. .Zip(Enumerable.Range(0, list.Count), (item, insertOrder) => new { Item = item, InsertOrder = insertOrder })
  42. .OrderBy(x => x.Item.Key)
  43. .ThenBy(x => x.InsertOrder)
  44. .Select(x => x.Item)
  45. .ToList();
Add Comment
Please, Sign In to add comment