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;  ...

iOS Dev: Encryption in Objective-C

Hello Friends: In this Article/Post, I introduced the one encryption technique in Objective-C.  Encryption Component Features in all  Symmetric Encryption: AES, Blowfish, Twofish, RC2, ARC4, DES, 3DES, PBES1, PBES2. Hash Algorithms :  SHA-1 , SHA256, SHA384, SHA512, MD2, MD4, MD5, HAVAL. Hash Algorithms: RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320. Encoding: Base64, hex, quoted-printable,  URL-encoding . HMAC with any supported hash algorithm: HMAC-MD5,  HMAC-SHA1 , etc. Password-based Key Derivation Functions: PBKDF1, PBKDF2 PKCS7 -- P7S and P7M creation, decryption, verification. Public key encryption/decryption with digital certificates. Digital signature creation/verification with digital certificates. Bzip2 in-memory compression. Encrypt / decrypt strings or byte data. Return encrypted data as Base64, quoted-printable, or hex-encoded strings. Hash strings or binary data using SHA1, MD2, MD5, HAVAL, SHA384, or SHA512. Public-key encryp...

iPhonegap: Developing a PhoneGap Application

Tutorial: Developing a PhoneGap Application Reference :  Here In this tutorial, you create a fully functional employee directory application with  PhoneGap . You will learn: How to use different local data storage strategies. How to use several PhoneGap APIs such as Geolocation, Contacts, and Camera. How to handle specific mobile problems such as touch events, scrolling, styling, page transitions, etc. How to build an application using a single page architecture and HTML templates. How to build (compile and package) an application for 6 platforms using  PhoneGap Build . To complete this tutorial, all you need is a code editor, a modern browser, and a connection to the Internet. A working knowledge of HTML and JavaScript is assumed, but you don’t need to be a JavaScript guru. Setting Up Download the assets for the workshop  here . Unzip the file anywhere on your file system. If your code editor allows you to “open a directory”, open the phonegap...