Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IComparer Sorting multiple properties.

IComparer Sorting multiple properties.

Scheduled Pinned Locked Moved C#
algorithmshelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vijay Kulavade
    wrote on last edited by
    #1

    Hi Experts, I want to sort multiple properties in the collection using IComparer. I could explain below what exactly i am looking for. I want to sort FirstName By Ascending and LastName by Descending order and vice versa. I am not looking for below scenario. collection.Sort(FirstName, Ascending) collection.Sort(LastName, Descending) But i am looing for below scenario. collection.Sort(FirstName, Ascending, LastName, Descending). Please need help on this. Thanks. Vijay

    L 1 Reply Last reply
    0
    • V Vijay Kulavade

      Hi Experts, I want to sort multiple properties in the collection using IComparer. I could explain below what exactly i am looking for. I want to sort FirstName By Ascending and LastName by Descending order and vice versa. I am not looking for below scenario. collection.Sort(FirstName, Ascending) collection.Sort(LastName, Descending) But i am looing for below scenario. collection.Sort(FirstName, Ascending, LastName, Descending). Please need help on this. Thanks. Vijay

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, if you want to sort in a special way, you need to create a class that implements IComparer, which means it provides a method int Compare (Object x, Object y) where you first cast x and y to the appropriate class, then perform the comparison the way you want it. Example, assuming the collection holds instances of class Person:

      class MyPersonComparer: IComparer {
      int Compare(object x, object y) {
      Person p1=(Person)x;
      Person p2=(Person)y;
      int diff=string.compare(p1.LastName, p2.LastName); // highest priority field first
      if (diff==0) diff=string.compare(p2.FirstName, p1.FirstName); // reverse sort order
      return diff;
      }
      }

      BTW: you could embed the Compare method inside an existing class (e.g. Person itself). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      V 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, if you want to sort in a special way, you need to create a class that implements IComparer, which means it provides a method int Compare (Object x, Object y) where you first cast x and y to the appropriate class, then perform the comparison the way you want it. Example, assuming the collection holds instances of class Person:

        class MyPersonComparer: IComparer {
        int Compare(object x, object y) {
        Person p1=(Person)x;
        Person p2=(Person)y;
        int diff=string.compare(p1.LastName, p2.LastName); // highest priority field first
        if (diff==0) diff=string.compare(p2.FirstName, p1.FirstName); // reverse sort order
        return diff;
        }
        }

        BTW: you could embed the Compare method inside an existing class (e.g. Person itself). :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        V Offline
        V Offline
        Vijay Kulavade
        wrote on last edited by
        #3

        Thanks a lot Luc. This is exactly i was looking for. :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups