473,382 Members | 1,611 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,382 software developers and data experts.

comparer and comparable

Why do we have the IComparable and IComparer interfaces don't they do
the same thing (dispite having function which take in eitehr one and
two parameters respectivly). an you give an exaple of where I would
use one and not the other.

Thanks
Nick
Nov 15 '05 #1
1 7847
Nick <ni*********@hotmail.com> wrote:
Why do we have the IComparable and IComparer interfaces don't they do
the same thing (dispite having function which take in eitehr one and
two parameters respectivly). an you give an exaple of where I would
use one and not the other.


They don't do the same thing: IComparable says "I'm willing to be
compared with other things." IComparer says "I'm willing to compare two
things."

For instance, you might use IComparer to compare two strings by length,
whereas string itself implements IComparable to compare strings by
lexical order.

Basically, use IComparable to make it simple to (say) sort objects in
some kind of "default" way, and IComparer to sort objects usually of a
*different* class, in custom ways.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2

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

Similar topics

1
by: Eric Newton | last post by:
come on framework guys ;-) probably an oversight but lets fix it and get a simple default comparer into v1.2? ...
1
by: sho_nuff | last post by:
Hello all, I have a sorted list that contains items: foo1 foo2 foo3 foo10 foo11 foo12
0
by: Frank Wisniewski | last post by:
Hi All, I have created my own generic comparer object which implements IComparer and uses reflection. I have tested it and it seem to work fine but I am worried about the way I did it. My goal...
3
by: MattC | last post by:
Hi, I found this code somewhere on the net and need to make some alterations. public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(String...
2
by: cindy | last post by:
i build a string string eProduct = "#blah#ablah#bbbb"; StringBuilder sb = new StringBuilder(eProduct); eProduct = sb.Replace("#","").ToString(); string aProducts= eProduct.Split(';'); then I...
2
by: tommaso.gastaldi | last post by:
Hi, I need to change at runtime the Comparer of a SortedList of mine (it does not seem to expose a Comparer or Icomparer property). Do you know how can I do that (clearly without reconstructing...
1
by: ShaveDave27 | last post by:
Hi, I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday). Now...
2
by: Luc The Perverse | last post by:
Hello! I am trying to find a way to deal with Windows' case insensitivity without just forcing everything lowercase. While I am open to criticism on my method - my question relates to the...
6
by: Tony | last post by:
Hello! My first question: I just can't figure out what is the usefulness of Comparer.Default.Compare(somestring1, somestring2); because I can just the same use...
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.