Re: Why don't Lists have a Comparator?

From: Phillip Lord (p.lord_at_russet.org.uk)
Date: 04/11/04


Date: 11 Apr 2004 16:37:10 +0100


>>>>> "Joe" == Joe <sfjoe@BLOCKEDspamcop.net> writes:

  Joe> In article <c5bivo$2rccl9$1@ID-18956.news.uni-berlin.de>,
  Joe> tcn@spamgourmet.org says...
>> Hi!
>>
>> Why do Lists not have an optional Comparator? I don't quite
>> understand the sense of Collections.sort() - which even does dump
>> the list to an array and sort that array...
>>
>> Timo
>>

  Joe> Say you had a List of these objects:

  Joe> public class MyClass {
  Joe> private int someValue; private int anotherValue;
  Joe> }

  Joe> How would you sort those objects? If you look at the javadocs
  Joe> for the Collections class, you'll read that "All elements in
  Joe> the list must implement the Comparable interface". That's how
  Joe> Collections.sort() works
  Joe> - by calling the compareTo() method on the members.

The OP is asking why not allow for an optional comparitor object. This
way you could change the natural sorting of Objects. This means you
could sort according objects which did not implement Comparable
directly, or sort Comparable objects in a different way.

I'm not quite sure what the OP wants though. List itself does nothing
that requires Comparision. Methods which do require this (Like
Collections.sort()) normally have an overloaded method which does take
a Comparitor object. Conversely interfaces such as SortedMap do
support an external Comparitor. As does SortedSet which is probably
what he wants. Of course a Set with an order is really a list, so
perhaps this is a misnomer. But I guess they didn't want the ordered
update (add( Object, int )) methods they would have got from List.

Phil



Relevant Pages

  • Re: CArray and CList
    ... Once again, thank you for the valuable education, Joe! ... If you are inserting or deleting a lot, ... >>your lists are. ...
    (microsoft.public.vc.mfc)
  • Re: ASP.NET Authentication
    ... Typically you would use groups in the allow/deny tags in NT format ... > Joe Kaplan wrote: ... >>for your application than to resort to file system ACLs. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: More video poker paytable questions
    ... He lists 8-5-4 Bonus Poker ... Joe J in LV ... Ace, Ten, and one high suited ...
    (alt.vacation.las-vegas)
  • Re: R.E.M. and Rush?
    ... > Joe wrote: ... >> My two favorite groups are R.E.M. and Rush. ... >> Anyone else have this unique combination in their favorite lists? ... Prev by Date: ...
    (rec.music.rem)