using System; using System.Collections.Generic; using System.Linq; using System.Text; //using System.Drawing; namespace LambdaForSort { struct Point { public int X, Y; public Point(int X, int Y) { this.X = X; this.Y = Y; } } class Program { static void Main(string[] args) { List<Point> Pts = new List<Point>(); Random R = new Random(0); const int N = 8; Pts.Add(new Point(100, 101)); for (int i = 0; i < 16; i++) { Pts.Add(new Point(R.Next(N), R.Next(N))); } Pts.Add(new Point(100, 99)); Console.Write("Orignal:"); foreach (Point P in Pts) { Console.Write("[{0},{1}] ", P.X, P.Y); } Console.WriteLine(); Pts.Sort((P1, P2) => { if (P1.X > P2.X) return 1; if (P1.X < P2.X) return -1; if (P1.Y > P2.Y) return 1; if (P1.Y < P2.Y) return -1; return 0; }); Console.Write("Sorted:"); foreach (Point P in Pts) { Console.Write("[{0},{1}] ", P.X, P.Y); } return; } } }
Standard input is empty
Orignal:[100,101] [7,2] [7,1] [0,4] [6,5] [6,6] [7,3] [0,5] [2,6] [6,3] [7,3] [2,5] [5,1] [0,1] [6,5] [5,5] [0,0] [100,99] Sorted:[0,0] [0,1] [0,4] [0,5] [2,5] [2,6] [5,1] [5,5] [6,3] [6,5] [6,5] [6,6] [7,1] [7,2] [7,3] [7,3] [100,99] [100,101]