|
-
Dec 12th, 2006, 05:48 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [vs 2003] help with ArrayList.sort and IComparer
I have an arraylist and i'd like to sort them in 3 different ways. I know how to make it sort just ONE way by putting these codes in my class:
Code:
int IComparable.CompareTo(object obj)
{
FoneStruct objFone = (FoneStruct)obj;
string str1 = this.name + this.number;
string str2 = objFone.name + objFone.number;
if(str1 == str2)
return 0;
else
return str1.CompareTo(str2);
}
and I just call ArrayList.sort() and it does it fine. But I would like to sort my arraylist based on different properties in my class. I noticed there is a ListArray.sort(Icomparer) function call. I'm guessing that is the key to the solution to my problem. But how do I use it?
-
Dec 12th, 2006, 06:23 PM
#2
Re: [vs 2003] help with ArrayList.sort and IComparer
The IComparer interface has a Compare method. The idea is that you declare your own class that implements IComparer and thus its Compare method. When you pass an instance of your IComparer to the ArrayList.Sort method its Compare method is called repeatedly on each pair of items to place them in the order defined by your method of comparison. Here's an example of an IComparer that sorts DateTime objects by time first, then date:
VB Code:
Public Class TimeAndDateComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim dt1 As Date = CDate(x)
Dim dt2 As Date = CDate(y)
'Compare the time components.
Dim result As Integer = dt1.TimeOfDay.CompareTo(dt2.TimeOfDay)
If result = 0 Then
'The times are the same so compare the date components.
result = dt1.Date.CompareTo(dt2.Date)
End If
Return result
End Function
End Class
I could pass an instance of that class to the Sort method of any ArrayList that contained only Date objects and it would sort them by time then date.
You can implement your Compare method in as simple or as complex a way as you like. It can compare objects of different types if you like, but it's up to you to specify what types are valid and how they are compared. You can also implement it such that it compares only one type but in different ways, depending on the properties of the objects themselves, which sounds like what you want.
If the type of the items in the ArrayList implements the IComparable interface then you can call the CompareTo method of the first object being compared as I have above.
-
Dec 12th, 2006, 06:25 PM
#3
Re: [vs 2003] help with ArrayList.sort and IComparer
B*gger! Forget I was in the C# forum. Here's a C# transaltion of the code above:
Code:
public class TimeAndDateComparer : IComparer
{
public int Compare(object x, object y)
{
DateTime dt1 = (DateTime)x;
DateTime dt2 = (DateTime)y;
// Compare the time components.
int result = dt1.TimeOfDay.CompareTo(dt2.TimeOfDay);
if (result == 0)
{
// The times are the same so compare the date components.
result = dt1.Date.CompareTo(dt2.Date);
}
return result;
}
}
Last edited by jmcilhinney; Dec 12th, 2006 at 06:30 PM.
-
Dec 13th, 2006, 05:45 PM
#4
Thread Starter
Frenzied Member
Re: [vs 2003] help with ArrayList.sort and IComparer
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
|