Wednesday, May 15, 2013

How to sort List of objects on it's property?

Guys, sometimes, in .Net, we stuck in a weird situation where we need to sort List based on property.
Here, we can argue on Enumerable OrderBy() method to sort the list on property value.

//
    // Summary:
    //     Sorts the elements of a sequence in ascending order according to a key.
    //
    // Parameters:
    //   source:
    //     A sequence of values to order.
    //
    //   keySelector:
    //     A function to extract a key from an element.
    //
    // Type parameters:
    //   TSource:
    //     The type of the elements of source.
    //
    //   TKey:
    //     The type of the key returned by keySelector.
    //
    // Returns:
    //     An System.Linq.IOrderedEnumerable<TElement> whose elements are sorted according
    //     to a key.
    //
    // Exceptions:
    //   System.ArgumentNullException:
    //     source or keySelector is null.
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);


But, here the point to be noted is that OrderBy() sorts list alphabetically not alphanumerically. (see example below).
If we want to sort our list alphanumerically, then we need to implement a class implemented IComparer interface.
Below is the class that compares property bit by bit and returns integer value depending on the compare result.

namespace SortingList
{
  /// <summary>
  /// Compares two strings, taking numeric chunks into account.  For
  /// example, "Hotel2" will be less than "Hotel-10";  Case-sensitivity
  /// is considered if and only if the strings are equivalent without regard to case.
  /// </summary>
  /// <returns>
  ///   Less than 0 if s1 is less than s2;
  ///   0 if s1 and s2 are equivalent;
  ///   Greater than 0 if s1 is greater than s2.
  /// </returns>
  public class LogicalHotelCompare : IComparer<Hotel>
  {
    public int Compare(Hotel x, Hotel y)
    {
      return DoCompare(x.Name, y.Name);
    }
    /// <summary>
    /// Compares two string alphanumerically. Case-sensitivity
    /// is considered if and only if the strings are equivalent without regard to case.
    /// </summary>
    /// <param name="s1">First String</param>
    /// <param name="s2">Second String</param>
    /// <returns>0 if strings are equal, negative value if s2 is bigger, possitive value if s1 is bigger.</returns>
    public static int DoCompare(string s1, string s2)
    {
      int c = DoCompare(s1, s2, true);
      if (c != 0) return c;
      return DoCompare(s1, s2, false);
    }

    public static int DoCompare(string s1, string s2, bool ignoreCase)
    {
      int i1 = 0;
      int i2 = 0;
      while (true)
      {
        string c1 = ReadNextChunk(s1, ref i1);
        string c2 = ReadNextChunk(s2, ref i2);
        if (c1 == null)
          return (c2 == null) ? string.Compare(s1, s2, ignoreCase) : -1;
        if (c2 == null) return 1;
        int c = CompareChunk(c1, c2, ignoreCase);
        if (c != 0) return c;
      }
    }

    static bool IsDigit(char c)
    {
      // We don't use Char.IsDigit because it includes some non-ASCII Unicode digits.
      return ('0' <= c) && (c <= '9');
    }

    static string ReadNextChunk(string s, ref int i)
    {
      if ((s == null) || (i >= s.Length)) return null;
      int start = i;
      bool isDigit = IsDigit(s[i++]);
      while ((i < s.Length) && IsDigit(s[i]) == isDigit) i++;
      return s.Substring(start, i - start);
    }

    static int CompareChunk(string c1, string c2, bool ignoreCase)
    {
      if (IsDigit(c1[0]))
      {
        if (!IsDigit(c2[0])) return -1;
        string s1 = StripLeadingZeros(c1);
        string s2 = StripLeadingZeros(c2);
        int c = s1.Length - s2.Length;
        if (c != 0) return c;
        return string.CompareOrdinal(s1, s2);
      }
      if (IsDigit(c2[0])) return 1;
      return string.Compare(c1, c2, ignoreCase);
    }

    static string StripLeadingZeros(string s)
    {
      if (s[0] != '0') return s;
      for (int i = 1; i < s.Length; i++)
        if (s[i] != '0') return s.Substring(i);
      return "";
    }
  }
}


Below is the example how we can see exact difference between OrderBy() and Sort() with IComparer methods. You can download example from here.
Example:
Hotel class that we will add into the list and will sort on property “Name”.

namespace SortingList
{
  /// <summary>
  /// Hotel class the we will use for sorting.
  /// </summary>
  public class Hotel
  {
    public string Name { get; set; }
    public int Rooms { get; set; }

    public Hotel(string name, int rooms)
    {
      this.Name = name;
      this.Rooms = rooms;
    }
  }
}

Program class have Main() method that will create different Hotel instances with different names and will add to the list.
Then we will use OrderBy() and our Sort() methods to sort the same list. Will use Display() method to display the sorted list.

namespace SortingList
{
  class Program
  {

    static void Main(string[] args)
    {
      List<Hotel> _hotelList = new List<Hotel>();
      List<Hotel> _hotelOrderBy;
      List<Hotel> _hotelSortWithCompare = new List<Hotel>();

      Hotel hotel1 = new Hotel("Hotel01", 50);
      Hotel hotel2 = new Hotel("Hotel2", 50);
      Hotel hotel3 = new Hotel("Hotel10", 50);
      Hotel hotel4 = new Hotel("Hotel11", 50);
      Hotel hotel5 = new Hotel("Hotel3", 50);

      _hotelList.Add(hotel1);
      _hotelList.Add(hotel2);
      _hotelList.Add(hotel3);
      _hotelList.Add(hotel4);
      _hotelList.Add(hotel5);

      //using order by
      _hotelOrderBy = _hotelList.OrderBy(s => s.Name).ToList();
      Console.WriteLine("OrderBy() Method ");
      Console.WriteLine("*******************");
      Display(_hotelOrderBy);
      Console.WriteLine("*******************");
      Console.WriteLine();
      Console.WriteLine();

      //using Sort
      LogicalHotelCompare lc = new LogicalHotelCompare();
      _hotelSortWithCompare.AddRange(_hotelList);
      _hotelSortWithCompare.Sort(lc);
      Console.WriteLine("Sort() with Comparer");
      Console.WriteLine("*******************");
      Display(_hotelSortWithCompare);
      Console.WriteLine("*******************");

      Console.ReadKey();
    }

    static void Display(List<Hotel> list)
    {
      foreach (var item in list)
      {
        Console.WriteLine(item.Name);
      }
    }
  }
}


Result :


You can clearly see the difference of different sorting in above result.
Enjoy!

No comments:

Post a Comment