- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
-
- namespace ConsoleApplication1
- {
- class Program
- {
- ///
- /// 独自に作成したデータクラスをLISTに格納し、LINQを使ってLISTをソートするサンプル
- ///
- ///
- static void Main(string[] args)
- {
- List list = new List();
-
- TEST t1 = new TEST();
- t1.x = 1;
- t1.y = 1;
- t1.str = "AB";
-
- TEST t2 = new TEST();
- t2.x = 1;
- t2.y = 1;
- t2.str = "CD";
-
- TEST t3 = new TEST();
- t3.x = 1;
- t3.y = 2;
- t3.str = "EF";
-
- TEST t4 = new TEST();
- t4.x = 1;
- t4.y = 2;
- t4.str = "GH";
-
- TEST t5 = new TEST();
- t5.x = 2;
- t5.y = 1;
- t5.str = "IJ";
-
- TEST t6 = new TEST();
- t6.x = 2;
- t6.y = 2;
- t6.str = "KL";
-
- TEST t7 = new TEST();
- t7.x = 3;
- t7.y = 1;
- t7.str = "MN";
-
- list.Add(t7);
- list.Add(t6);
- list.Add(t5);
- list.Add(t4);
- list.Add(t3);
- list.Add(t2);
- list.Add(t1);
-
- Console.WriteLine("【ソート前のリストの中身】");
- foreach (TEST t in list)
- {
- Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
- }
-
- Console.WriteLine("\n【xでソート】");
- List list2 = list.OrderBy(n => n.x).ToList();
- foreach (TEST t in list2)
- {
- Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
- }
-
- Console.WriteLine("\n【x、yでソート】");
- List list3 = list.OrderBy(n => n.x).ThenBy(n => n.y).ToList();
- foreach (TEST t in list3)
- {
- Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
- }
-
- Console.WriteLine("\n【x、y、strでソート】");
- List list4 = list.OrderBy(n => n.x).ThenBy(n => n.y).ThenBy(n => n.str).ToList();
- foreach (TEST t in list4)
- {
- Console.WriteLine("x=" + t.x + " y=" + t.y + " str=" + t.str);
- }
-
- Console.ReadLine();
-
- }
- }
-
- ///
- /// Listに格納する独自のデータクラス
- ///
- class TEST
- {
- public int x { get; set; }
- public int y { get; set; }
- public String str { get; set; }
- }
- }
|