CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Posts
    16

    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!

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    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?

  3. #3
    Join Date
    Oct 2009
    Posts
    16

    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.

  4. #4
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    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)    
            }
    }

  5. #5
    Join Date
    Oct 2009
    Posts
    16

    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])    
            }
    }

  6. #6
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    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
  •  





Click Here to Expand Forum to Full Width

Featured