Posted By

qrist0ph on 10/08/08


Tagged

lambda Net


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
khouser
dmertl


.Net List Sorting with Lambda Functions


 / Published in: C#
 

  1. private static void sortArray()
  2. {
  3. List<TestKlasse> liste = new List<TestKlasse>();
  4. liste.Add(new TestKlasse() { X = 2, Y = 8 });
  5. liste.Add(new TestKlasse() { X = 0, Y = 10 });
  6. liste.Add(new TestKlasse() { X = 1, Y = 9 });
  7. liste.Add(new TestKlasse() { X = 3, Y = 7 });
  8. // with delegate
  9. liste.Sort(delegate(TestKlasse a, TestKlasse b) { return a.Y.CompareTo(b.Y); });
  10. liste.ForEach(delegate(TestKlasse tk){Console.WriteLine("X {0}, Y {1}",tk.X,tk.Y);});
  11. // with lambda function
  12. liste.Sort((a, b) => a.X.CompareTo(b.X));
  13. liste.ForEach(delegate(TestKlasse tk) { Console.WriteLine("X {0}, Y {1}", tk.X, tk.Y); });
  14. }

Report this snippet  

You need to login to post a comment.