473,383 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

Jul 11 '06 #1
4 1308
Instead of using ArrayList.Sort(), you really should call
ArrayList.Sort(IComparer), and pass in different implementation of
IComparer.

HTH.
ch**********@googlemail.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
Jul 11 '06 #2
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

Jul 11 '06 #3
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(false)); //By Y value
/claes

"Riddle" <ch**********@googlemail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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

Jul 11 '06 #4
A ha... Thanks very much for you help.
I understand now.

Thanks again!
Chris

Jul 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cybertof | last post by:
Hello, Could someone please explain me the difference between the 2 interfaces IComparable and IComparer ? In which cases use one or the other ? Are they dedicated to own classes or built-in...
10
by: INeedADip | last post by:
I am trying to use a generic (reflection) IComparer class to sort a generic list but I get the error: Unable to cast object of type 'GenericComparer' to type 'System.Collections.Generic.Icomparer...
1
by: INeedADip | last post by:
I am trying to use the following generic (reflection) class as the ICamparer parameter for a generic list..but I get the error: "Unable to cast object of type 'GenericComparer' to type...
11
by: solandre | last post by:
my motivation is as follows: i have to binarysearch several million times an long that is several million items big. this costs time, although i use Array.Binarysearch<long>. so i try to keep...
1
by: Brett Romero | last post by:
What are the reasons to use one over the other? SortedList implements IComparer.Compare(). Why would you also want to create a SortedList class that implements IComparable.CompareTo()? I know...
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
10
by: Tony | last post by:
Hello! I'm reading in a book and this can't be correct it says. "Objects passed to Comparer.Compare() are checked to see if they support IComparable. If they do, then that implementation is...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
5
by: xtech1005 | last post by:
This is probably a newbie question but I'm really confused on how the IComparer works and I've looked for information online and offline. The code works, I just don't understand how. I'm sorting...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.