-
April 12th, 2011, 10:17 AM
#1
sorting issue icomparer
Hi,
icomparer confuses me!
I have a class, very simple put:
Code:
public class myClass{
int[] ints1;
int[] ints2;
}
Now I have an array of myClass[] and would like to sort the array on ints[2].
Is that possible?
Thanks!
-
April 12th, 2011, 10:47 AM
#2
Re: sorting issue icomparer
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?
-
April 12th, 2011, 12:38 PM
#3
Re: sorting issue icomparer
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.
-
April 12th, 2011, 12:45 PM
#4
Re: sorting issue icomparer
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)
}
}
-
April 12th, 2011, 04:21 PM
#5
Re: sorting issue icomparer
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])
}
}
-
April 12th, 2011, 04:48 PM
#6
Re: sorting issue icomparer
Let me know how it goes.
I prefer to you use List<myClass> instead of myClass[] because list has the Sort method on it.
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();
Alternatively use Array.Sort(instances) if using a traditional array.
Both will use your CompareTo method.
You can also define multiple sort methods that can be passed to a sort method.
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])
}
}
You can then use your special sorting methods for your sorting:
Code:
Array.Sort(instances, myClass.SpecialSort)
Last edited by RedBully; April 12th, 2011 at 04:49 PM.
Reason: Typo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|