IComparer problem
-
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.
-
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.
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] -
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.
-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) -
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.
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 Memberspublic 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
RabbitOne 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
-
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 Memberspublic 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
RabbitOne 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
-
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.
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