fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int[] keys = { 31, 28, 31, 30, 31 };
  10. string[] values = { "Jan", "Feb", "Mar", "Apr", "Jan" };
  11.  
  12. var list1 = new List<KeyValuePair<int, string>>();
  13. var list2 = new List<Disp>();
  14. Console.WriteLine("テストデータ");
  15. for (int i = 0; i < keys.Length; i++)
  16. {
  17. list1.Add(new KeyValuePair<int, string>(keys[i], values[i]));
  18. list2.Add(new Disp(keys[i], values[i]));
  19. Console.WriteLine("{0} {1}", keys[i], values[i]);
  20. }
  21.  
  22. Console.WriteLine("汎用");
  23. foreach (var disp in list1.OrderBy(t => -t.Key))
  24. {
  25. Console.WriteLine("{0} {1}", disp.Key, disp.Value);
  26. }
  27.  
  28. Console.WriteLine("独自クラス");
  29. foreach (var disp in list2.OrderBy(t => -t.Key))
  30. {
  31. Console.WriteLine("{0} {1}", disp.Key, disp.Value);
  32. }
  33. }
  34.  
  35. class Disp
  36. {
  37. public int Key { get; set; }
  38. public string Value { get; set; }
  39. public Disp(int key, string value)
  40. {
  41. Key = key;
  42. Value = value;
  43. }
  44. }
  45. }
  46.  
Success #stdin #stdout 0.05s 34080KB
stdin
Standard input is empty
stdout
テストデータ
31 Jan
28 Feb
31 Mar
30 Apr
31 Jan
汎用
31 Jan
31 Mar
31 Jan
30 Apr
28 Feb
独自クラス
31 Jan
31 Mar
31 Jan
30 Apr
28 Feb