aspose file tools*
The moose likes JSF and the fly likes List<SelectItem> sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "List<SelectItem> sort" Watch "List<SelectItem> sort" New topic
Author

List<SelectItem> sort

naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
Below is my code

List<SelectItem> listOfAlbums = new ArrayList<SelectItem>();

<h:selectOneMenu value="#{bean.selectedValue}" style="width:350px;" valueChangeListener="#{bean.valueChangeDoSomething}">
<f:selectItems value="#{bean.listOfAlbums}"/>
</h:selectOneMenu>



My Question is, whenever i add a new value, how can sort the list or insert the value in appropriate order

Generally we use following statement

Collections.sort(listOfAlbums,Collections.reverseOrder());


but in my case it fails, Since "SelectItem" doesn't implement Comparable interface

any ideas.........please...............
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
If it has been following variable defined then sort works fine

List<String> listOfAlbums = new ArrayList<String>();

But i have to define ArrayList as "SelectItem" since i am using this ArrayList in the dropdown in my JSF Code
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

You have a choice of not using a collection of SelectItems to populate your selectItem list,

for example, the following iterates over an array of ColorChoice objects (You can use List or Map as well)

OR

since SelectItem does not implement comparable, you can create a custom comparator and use THAT to sort as described here:
http://stackoverflow.com/questions/5007959/how-to-sort-a-selectitem-list-based-on-labels-in-java
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
good information

i will try to implement custom comparator and will see how it goes
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Tim McGuire wrote:You have a choice of not using a collection of SelectItems to populate your selectItem list,

for example, the following iterates over an array of ColorChoice objects (You can use List or Map as well)

OR

since SelectItem does not implement comparable, you can create a custom comparator and use THAT to sort as described here:
http://stackoverflow.com/questions/5007959/how-to-sort-a-selectitem-list-based-on-labels-in-java


Actually, as far as I'm aware, this feature (new in JSF2) is just a convenience that permits the SelectItem collection to be constructed automatically.

Technically, it's not an iterator, either. It's an expression of a linear collection (or array), just as a dataTable is an expression of a 2-dimensional collection. LISPers and other such evil people will be glad to point out that actual iteration to express such constructs is purely optional. Don't you just love pedantry? :)


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List<SelectItem> sort