• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List<SelectItem> sort

 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good information

i will try to implement custom comparator and will see how it goes
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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? :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic