commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Colebourne <>
Subject Re: [Collections] Bag sorted by count
Date Thu, 08 Jun 2006 07:50:03 GMT
Paul Dlug wrote:
> 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.

Hmmm. I've just taken a look at the code, and its not as simple as I 
thought. I believe that we could take your methods as you propose and it 
would work, however, I'm not sure thats the best way.

My problem is that the methods involve copying the data to another list 
(List not Object[]) to return it. It feels like a core bag competancy.

Really, this would seem to be a SortedBag (TreeBag) where the comparator 
checks the count. But it could be hard to write a TreeBag comparator 
that does this (I haven't really checked). Thus the whole bag is kept 
sorted by count, and normal bag methods can then be directly used to 
extract the data.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message