IComparer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christriddle@googlemail.com

    IComparer question

    Hi,

    I want to be able to sort a ArrayList of Points, for example, in one of
    two (or more) ways depending of the nature of the Points in the
    ArrayList. Basically I want two different comparers, both of which I
    could use when I want. E.g. one to sort the points in order of their X
    value and another for their Y value.

    I have no trouble setting up a single comparer, but I don't know how do
    accomplish the task above. Any help would be appreciated!!

    For example, the code I would like to use:

    if (MaxX(Points) < 0)
    Points.Sort(); //By X value
    else
    Points.Sort(); //By Y value


    Thanks in advance...
    Chris Riddle

  • Jianwei Sun

    #2
    Re: IComparer question

    Instead of using ArrayList.Sort( ), you really should call
    ArrayList.Sort( IComparer), and pass in different implementation of
    IComparer.

    HTH.
    christriddle@go oglemail.com wrote:
    Hi,
    >
    I want to be able to sort a ArrayList of Points, for example, in one of
    two (or more) ways depending of the nature of the Points in the
    ArrayList. Basically I want two different comparers, both of which I
    could use when I want. E.g. one to sort the points in order of their X
    value and another for their Y value.
    >
    I have no trouble setting up a single comparer, but I don't know how do
    accomplish the task above. Any help would be appreciated!!
    >
    For example, the code I would like to use:
    >
    if (MaxX(Points) < 0)
    Points.Sort(); //By X value
    else
    Points.Sort(); //By Y value
    >
    >
    Thanks in advance...
    Chris Riddle
    >

    Comment

    • Riddle

      #3
      Re: IComparer question

      Thanks for the quick reply.

      This is what I thought originally, but how do I implment this new
      Icomparer? Is it external to the class I want to compare? Sorry, having
      a bit of a slow day today!

      Thanks,
      Chris

      Comment

      • Claes Bergefall

        #4
        Re: IComparer question

        You can implement it anywhere you'ld like. Have its constructor take an
        argument specifying the sort order that you want to use. Something like
        this:

        public class MyComparer : IComparer
        {
        private bool m_sortByX;
        public MyComparer(bool sortByX)
        {
        m_sortByX = sortByX;
        }
        public int Compare(object x, object y)
        {
        // Sort in different order depending on m_sortByX
        }
        }
        ....
        if (MaxX(Points) < 0)
        Points.Sort(new MyComparer(true )); //By X value
        else
        Points.Sort(new MyComparer(fals e)); //By Y value


        /claes

        "Riddle" <christriddle@g ooglemail.comwr ote in message
        news:1152628995 .482700.121670@ m73g2000cwd.goo glegroups.com.. .
        Thanks for the quick reply.
        >
        This is what I thought originally, but how do I implment this new
        Icomparer? Is it external to the class I want to compare? Sorry, having
        a bit of a slow day today!
        >
        Thanks,
        Chris
        >

        Comment

        • Riddle

          #5
          Re: IComparer question

          A ha... Thanks very much for you help.
          I understand now.

          Thanks again!
          Chris

          Comment

          Working...