Hi,
icomparer confuses me!
I have a class, very simple put:
Now I have an array of myClass[] and would like to sort the array on ints[2].Code:public class myClass{
int[] ints1;
int[] ints2;
}
Is that possible?
Thanks!
Printable View
Hi,
icomparer confuses me!
I have a class, very simple put:
Now I have an array of myClass[] and would like to sort the array on ints[2].Code:public class myClass{
int[] ints1;
int[] ints2;
}
Is that possible?
Thanks!
Each class has an array of ints in it
e.g. myClass 1 could have an ints2 array of 1,2,3,4,5
e.g. myClass 2 could have an ints2 array of 1,2,3,4,5
In this case, when you sort your array of myClass, what should come first, myClass 1 or myClass2?
That is an impossible situation considering how these arrays are build. But if it would be possible, then it doesn't really matter how this is handled.
You need to inherit from IComparer or IComparer<myClass>
e.g.
Code:public class myClass : IComparable<myClass>
{
int[] ints1;
int[] ints2;
public int CompareTo(myClass other)
{
//Put in comparision between classes here.
//Not sure what logic you actually want to implement
//to decide if 'this' comes before or after 'other'?
//I'm going to sort based on a comparision of the length
//of the int2 array but you can change this to some other logic.
return ints2.Length.CompareTo(other.ints2.Length)
}
}
Thanks a lot RedBully!
I will try tomorrow, around midnight over here.
It has to sort on ints2 and then ideally a specific element I can pass, something like this?
Code:public class myClass : IComparable<myClass>
{
int[] ints1;
int[] ints2;
public int CompareTo(myClass other, int element)
{
//Put in comparision between classes here.
//Not sure what logic you actually want to implement
//to decide if 'this' comes before or after 'other'?
//I'm going to sort based on a comparision of the length
//of the int2 array but you can change this to some other logic.
return ints2[element].CompareTo(other.ints2[element])
}
}
Let me know how it goes.
I prefer to you use List<myClass> instead of myClass[] because list has the Sort method on it.
Alternatively use Array.Sort(instances) if using a traditional array.Code:List<myClass> instances = new List<myClass>();
instances.Add(new myClass());
instances.Add(new myClass());
//Call sort on instances and it will use the sort logic
//defined by the CompareTo method in the myClass class.
instances.Sort();
Both will use your CompareTo method.
You can also define multiple sort methods that can be passed to a sort method.
You can then use your special sorting methods for your sorting:Code:public class myClass : IComparable<myClass>
{
int[] ints1;
int[] ints2;
public static int SpecialSort(myClass a, myClass b)
{
return a.ints1.Length.CompareTo(b.ints1.Length);
{
public int CompareTo(myClass other, int element)
{
//Put in comparision between classes here.
//Not sure what logic you actually want to implement
//to decide if 'this' comes before or after 'other'?
//I'm going to sort based on a comparision of the length
//of the int2 array but you can change this to some other logic.
return ints2[element].CompareTo(other.ints2[element])
}
}
Code:Array.Sort(instances, myClass.SpecialSort)