Results 1 to 4 of 4

Thread: [RESOLVED] [vs 2003] help with ArrayList.sort and IComparer

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class TimeAndDateComparer
    2.     Implements IComparer
    3.  
    4.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    5.         Dim dt1 As Date = CDate(x)
    6.         Dim dt2 As Date = CDate(y)
    7.  
    8.         'Compare the time components.
    9.         Dim result As Integer = dt1.TimeOfDay.CompareTo(dt2.TimeOfDay)
    10.  
    11.         If result = 0 Then
    12.             'The times are the same so compare the date components.
    13.             result = dt1.Date.CompareTo(dt2.Date)
    14.         End If
    15.  
    16.         Return result
    17.     End Function
    18.  
    19. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: [vs 2003] help with ArrayList.sort and IComparer

    Thanks, it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width