• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am getting a Collection from a method which I am casting to a List:

List active = (List)this.getCapacityService().getBranches();

I would like to sort the list in alphabetical order.

I can't use Collections.sort(active) because I need to return a collection.

Can someone assist on how I can resolve this?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand why you can't use Collections.sort(List list)? Why should the need to return a Collection make any difference?
 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is I need the collection for another method which takes a collection as an argument:

form.setBranchesList(active,"brnId","brnName");

Basically I have:

Collection active = this.getCapacityService().getBranches();

so now I would need to pass a sorted list/collection to:

setBranchesList(active,"brnId","brnName");

(i.e active has to be a sorted Collection and returning a collection and not void as is the case with Collections.sort)

Is this clearer now?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sort method has a void return type because it mutates the given list. If this doesn't work:

List active = (List)this.getCapacityService().getBranches();
Collections.sort(active);
setBranchesList(active,"brnId","brnName");

... probably because getCapacityService returns a Collection which isn't also of type List, or the collection returned is read-only and can't be permuted, why not make a copy and sort that:

Collection result= this.getCapacityService().getBranches();
List active = new ArrayList(result);
Collections.sort(active);
setBranchesList(active,"brnId","brnName");
 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem on setBranchesList because it takes (Collection col, String str, String str1) as its arguments therefore in the last part of my code:

setBranchesList(active,"brnId","brnName"); active should be a Collection where as if I use active from Collections.sort(active), it will be void.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Patrick --

Listen carefully:

Collections.sort() sorts the collection you pass to it. Afterwards, the collection you passed as an argument is sorted. Any previous variables that pointed to it still point to it. Use them rather than the (nonexistent) return value of sort():

List active = ...
Collections.sort(active);
anythingElseYouWant(active); // <--- See? We can still use "active"

Anyway, this conversation does not belong in our "advanced Java" forum; moving it to Java in General (Beginner).
 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Ernest but I am fully aware of what you are saying and I know that after Collections.sort, you can still refer to active.

Hear me out, I will give you more detail.


I have an interface called ListForm which has
public void setBranchesList(java.util.Collection items, String str1, String str2);


Then I have this method in my class:

public void listBranch(ActionMapping mapping, ListBranchForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

Collection result = this.getCapacityService().getBranches();
List active = new ArrayList(result);
Collections.sort(active);

form.setBranchesList(active,"brnId","brnName");
}


The problem was on form.setBranchesList(active,"brnId","brnName");

If I use form.setBranchesList(result,"brnId","brnName"); I get a list that is not sorted, but once I use sort, it doesn't work.

I have now used:

Collections.sort(active, new Comparator(){
public int compare(Object element1,Object element2) {
//my implementation
}
});
form.setBranchesList(active,"brnId","brnName");

and it works.

Thanks.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Patrick Mugabe:

but once I use sort, it doesn't work.



Doesn't work? How?

Originally posted by Patrick Mugabe:

I have now used:

Collections.sort(active, new Comparator(){
public int compare(Object element1,Object element2) {
//my implementation
}
});
form.setBranchesList(active,"brnId","brnName");

and it works.



The other shoe drops. Your problem in sorting seems to be that the elements in the list are not Comparable, or that you want to sort them in another way than their "natural" ordering. But note that you never posted this. You never posted your specific error. It's important to post good questions in order to get good answers. Otherwise, with the best intentions, forum members have to resort to mindreading.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Patrick",

I don't know why nobody mentioned it before, but your screen name does not quite live up to our Naming Policy. We require a real sounding first AND last name. Please go here and update your screen name.

thanks!!!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:

I don't know why nobody mentioned it before,



Because he just changed it; it was something like "patrick mugabe" before.
reply
    Bookmark Topic Watch Topic
  • New Topic