commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Dlug" <paul.d...@gmail.com>
Subject Re: [Collections] Bag sorted by count
Date Wed, 07 Jun 2006 23:40:57 GMT
On 6/7/06, Stephen Colebourne <scolebourne@btopenworld.com> wrote:
>
> Paul Dlug wrote:
> > Hopefully a simple question...
> >
> > Is there an implementation of Bag that returns its elements sorted by
> their
> > count? I can't seem to find an elegant way to implement this (it seems
> like
> > the comparator would need a reference to the Bag it is comparing
> elements
> > for to do this with a TreeBag).
>
> We have no such implementation. Its not a bad idea though.


I'm happy to provide a patch since I already have an implementation
completed. The best way I can think of to take this is to provide two
methods to AbstractMapBag:

public Object[] getSortedCount();

public Object[] getSortedCount(Comparator c);

Please let me know if List() is preferable to Object[], from the research
I've done so far it appears that a Set must be copied into a List or Array
in order to sort it. It seems to me that better performance would be
achieved with Set.toArray() and Arrays.sort()

The second method would be used to provide a second level comparator (first
sort by count, if there's a tie sort by Comparator). This is useful to do
something like sorting categories by count and then alphabetically.

This could alternatively be placed in BagUtils but I think it's cleaner to
have it as an instance method on the Bag itself.

Feedback? Alternate method names?

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message