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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. IComparer problem

IComparer problem

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 5 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.
  • L Offline
    L Offline
    livez
    wrote on last edited by
    #1

    Hello! I'm trying to have a custom-sorter class to sort my objects. I want my objects to always be in a certain order(not alphabetically). In my Compare(object a, object b) method I check which objects they are and return 1 or -1 depending on which one I want to come first. How exactly does it work, do I return -1 if I want object a before object b in my collection? It doesn´t seem consistent to me, some values get sorted correctly and some don´t.

    CPalliniC D J L 4 Replies Last reply
    0
    • L livez

      Hello! I'm trying to have a custom-sorter class to sort my objects. I want my objects to always be in a certain order(not alphabetically). In my Compare(object a, object b) method I check which objects they are and return 1 or -1 depending on which one I want to come first. How exactly does it work, do I return -1 if I want object a before object b in my collection? It doesn´t seem consistent to me, some values get sorted correctly and some don´t.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You probably should post your code and your test data. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • L livez

        Hello! I'm trying to have a custom-sorter class to sort my objects. I want my objects to always be in a certain order(not alphabetically). In my Compare(object a, object b) method I check which objects they are and return 1 or -1 depending on which one I want to come first. How exactly does it work, do I return -1 if I want object a before object b in my collection? It doesn´t seem consistent to me, some values get sorted correctly and some don´t.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        -1 if a less than b, 0 if a equal b, 1 if a greater than b.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        1 Reply Last reply
        0
        • L livez

          Hello! I'm trying to have a custom-sorter class to sort my objects. I want my objects to always be in a certain order(not alphabetically). In my Compare(object a, object b) method I check which objects they are and return 1 or -1 depending on which one I want to come first. How exactly does it work, do I return -1 if I want object a before object b in my collection? It doesn´t seem consistent to me, some values get sorted correctly and some don´t.

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          I wrote you a little sameple which might help. I have 3 types of pet:

          public class Dog { }
          public class Cat { }
          public class Rabbit { }

          I dont want to order these alphabetically, I want to order them in order of my preference of them as pets. I like cats more than dogs, and dogs more than rabbits. I could write a comparer to always order a list of pets in my prefered order:

          public class MyPetPreferenceComparer : IComparer
          {
          #region IComparer Members

          public int Compare(object x, object y)
          {
              // if they are same type return 0
              if (x.GetType() == y.GetType())
                  return 0;
          
              // I like cats best so if lhs is a cat it comes before whatever is rhs
              if (x is Cat)
              {
                  return -1;
              }
                          
              if(x is Dog)
              {
                  // lhs is a dog, if rhs is a rabbit dog comes before
                  if (y is Rabbit)
                      return -1;
                  // otherwise dog comes after
                  return 1;
              }
              // I like rabbits least so they always come after
              if (x is Rabbit)
                  return 1;
          
              return 0;
          }
          
          #endregion
          

          }

          Now the test code:

          ArrayList list = new ArrayList();
          list.Add(new Rabbit());
          list.Add(new Cat());
          list.Add(new Dog());
          list.Add(new Cat());
          list.Add(new Rabbit());
          list.Add(new Dog());

          list.Sort(new MyPetPreferenceComparer());
          foreach (object obj in list)
          {
          Console.WriteLine(obj.GetType().Name);
          }

          Outputs:

          Cat
          Cat
          Dog
          Dog
          Rabbit
          Rabbit

          One thing Id add - nowdays we tend to prefer the generic versions of these sorts of interface so try to use IComparer<T> in System.Collection.Generics

          L 1 Reply Last reply
          0
          • J J4amieC

            I wrote you a little sameple which might help. I have 3 types of pet:

            public class Dog { }
            public class Cat { }
            public class Rabbit { }

            I dont want to order these alphabetically, I want to order them in order of my preference of them as pets. I like cats more than dogs, and dogs more than rabbits. I could write a comparer to always order a list of pets in my prefered order:

            public class MyPetPreferenceComparer : IComparer
            {
            #region IComparer Members

            public int Compare(object x, object y)
            {
                // if they are same type return 0
                if (x.GetType() == y.GetType())
                    return 0;
            
                // I like cats best so if lhs is a cat it comes before whatever is rhs
                if (x is Cat)
                {
                    return -1;
                }
                            
                if(x is Dog)
                {
                    // lhs is a dog, if rhs is a rabbit dog comes before
                    if (y is Rabbit)
                        return -1;
                    // otherwise dog comes after
                    return 1;
                }
                // I like rabbits least so they always come after
                if (x is Rabbit)
                    return 1;
            
                return 0;
            }
            
            #endregion
            

            }

            Now the test code:

            ArrayList list = new ArrayList();
            list.Add(new Rabbit());
            list.Add(new Cat());
            list.Add(new Dog());
            list.Add(new Cat());
            list.Add(new Rabbit());
            list.Add(new Dog());

            list.Sort(new MyPetPreferenceComparer());
            foreach (object obj in list)
            {
            Console.WriteLine(obj.GetType().Name);
            }

            Outputs:

            Cat
            Cat
            Dog
            Dog
            Rabbit
            Rabbit

            One thing Id add - nowdays we tend to prefer the generic versions of these sorts of interface so try to use IComparer<T> in System.Collection.Generics

            L Offline
            L Offline
            livez
            wrote on last edited by
            #5

            thanks alot!

            1 Reply Last reply
            0
            • L livez

              Hello! I'm trying to have a custom-sorter class to sort my objects. I want my objects to always be in a certain order(not alphabetically). In my Compare(object a, object b) method I check which objects they are and return 1 or -1 depending on which one I want to come first. How exactly does it work, do I return -1 if I want object a before object b in my collection? It doesn´t seem consistent to me, some values get sorted correctly and some don´t.

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

              Hi, an int comparer could just return the difference, as in: public static int Compare(int a, int b) {return a-b;} BTW: you MUST return zero when both objects are identical. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - 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 the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              modified on Sunday, June 12, 2011 9:02 AM

              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