Sorting


Sort (System.Comparison<T> comparison)

Sorts the elements in a list by making use of the System.Comparison<T> delegate

public delegate int Comparison <in T>(T x, T y) 

This delegate must return a signed integer that indicates if
-1 - if x is less than y
1 - if y is less than x
0 - if x equals y


System.Collections.Generic.List<string> mylist; 
mylist = new System.Collections.Generic.List<string>();
mylist.Add("hhh");
mylist.Add("bbb");
mylist.Add("");
mylist.Add(null);
mylist.Add("aaa");
mylist.Add("kkk");
mylist.Sort(CompareMyItems);

private static int CompareMyItems(string x, string y)
{
   if (x==null)
   {
      if (y==null) { return 0; } //both null
      else { return 1; } //y > null
   }
   else
   {
      if (y==null) { return 1; } //x > null
      else
      {
         int retval = x.Length.CompareTo(y.Length);
         if (retval==0) { return x.CompareTo(y);
         else { return retval; }
      }
   }
}


© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext