独自のクラスが格納されたListをソートするサンプル
LINQを使用すれば、独自に作成したクラスの複数のプロパティでListのソートを行うことが可能。

  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         /// 
  11.         /// 独自に作成したデータクラスをLISTに格納し、LINQを使ってLISTをソートするサンプル
  12.         /// 
  13.         /// 
  14.         static void Main(string[] args)
  15.         {
  16.             List list = new List();
  17.             TEST t1 = new TEST();
  18.             t1.x = 1;
  19.             t1.y = 1;
  20.             t1.str = "AB";
  21.             TEST t2 = new TEST();
  22.             t2.x = 1;
  23.             t2.y = 1;
  24.             t2.str = "CD";
  25.             TEST t3 = new TEST();
  26.             t3.x = 1;
  27.             t3.y = 2;
  28.             t3.str = "EF";
  29.             TEST t4 = new TEST();
  30.             t4.x = 1;
  31.             t4.y = 2;
  32.             t4.str = "GH";
  33.             TEST t5 = new TEST();
  34.             t5.x = 2;
  35.             t5.y = 1;
  36.             t5.str = "IJ";
  37.             TEST t6 = new TEST();
  38.             t6.x = 2;
  39.             t6.y = 2;
  40.             t6.str = "KL";
  41.             
  42.             TEST t7 = new TEST();
  43.             t7.x = 3;
  44.             t7.y = 1;
  45.             t7.str = "MN";
  46.             list.Add(t7);
  47.             list.Add(t6);
  48.             list.Add(t5);
  49.             list.Add(t4);
  50.             list.Add(t3);
  51.             list.Add(t2);
  52.             list.Add(t1);
  53.             Console.WriteLine("【ソート前のリストの中身】");
  54.             foreach (TEST t in list)
  55.             {
  56.                 Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
  57.             }
  58.             Console.WriteLine("\n【xでソート】");
  59.             List list2 = list.OrderBy(n => n.x).ToList();
  60.             foreach (TEST t in list2)
  61.             {
  62.                 Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
  63.             }
  64.             Console.WriteLine("\n【x、yでソート】");
  65.             List list3 = list.OrderBy(n => n.x).ThenBy(n => n.y).ToList();
  66.             foreach (TEST t in list3)
  67.             {
  68.                 Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
  69.             }
  70.             Console.WriteLine("\n【x、y、strでソート】");
  71.             List list4 = list.OrderBy(n => n.x).ThenBy(n => n.y).ThenBy(n => n.str).ToList();
  72.             foreach (TEST t in list4)
  73.             {
  74.                 Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
  75.             }
  76.             Console.ReadLine(); 
  77.         }
  78.     }
  79.     /// 
  80.     /// Listに格納する独自のデータクラス
  81.     /// 
  82.     class TEST
  83.     {
  84.         public int x { get; set; }
  85.         public int y { get; set; }
  86.         public String str { get; set; }
  87.     }
  88. }

image

戻る