Skip to main content

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!

Comments

Popular posts from this blog

WPF-MVVM: RelayCommand Implementation

In WPF if we are implementing MVVM pattern then we need to play with Command rather than Events. You can use ICommand interface to create each command class. Implementation of ICommand in a class gives you CanExecute(), Execute() methods which take part in the action performed by Command.   Rather than making Command Class for each Command we can implement a generic Relay Command to get Command. Below is a RelayCommand class that we will implement.   ///   <summary>      ///  To register commands in MMVM pattern      ///   </summary>      class   RelayCommands  :  ICommand     {          readonly   Action < object > _execute;          readonly   Predicate < object > _canExecute;  ...

.Net List with Changed event

Sometimes we need a List which can notify user when an item is added. Here is the way that you can implement a generic ArrayList which notifies user at the time of an element is added.   using  System; using  System.Collections; namespace  ArchiveData.Logging {    // A delegate type for hooking up change notifications.    public   delegate   void   ChangedEventHandler ( object  sender,  EventArgs  e);    public   class   ListWithChangedEvent  :  ArrayList   {      // An event that clients can use to be notified whenever the      // elements of the list change.      public   event   ChangedEventHandler  Changed;      public   object  NewlyAddedItem {...

What is DispatcherTimer in wpf?

DispatcherTimer When you want to set a timer working with GUI, you always come across threading problem. The problem is that if you want to send some changes to UI that is constantly/continuously changing then that will make your UI unresponsive or in other words it will hang your UI.   To overcome from this situation, WPF gives us DispatcherTimer threading functionality that will take care of such continuously changing processing on UI thread and that will not hang your UI. We can accomplish same scenario in Win Form , through System.Windows.Forms.Timer and in WPF it is System.Windows.Threading.DispatcherTimer .   Difference between DispatcherTimer and Regular timer (System.Timers.Timer) DispatcherTimer is the regular timer. It fires its Tick event on the UI thread, you can do anything you want with the UI. System.Timers.Timer is an asynchronous timer, its Elapsed event runs on a thread pool thread. You have to be very careful in your event handler...