Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primefaces selectOneMenu not sorted

Tags:

java

I have a selectonemenu in my page and i get its contents from my backing bean. But contents of selectonemenu is not sorted.Here is the code...

    <p:selectOneMenu id="Department" value="#{search.selectedDepartment}">  
            <f:selectItem itemLabel="Select Department" itemValue="" />  
            <f:selectItems value="#{search.departments}" />  

        </p:selectOneMenu> 

and my bean...

public class SearchBean implements Serializable {

private Map<String, Map<String, String>> courseData = new HashMap<String, Map<String, String>>();

private Map<String, String> departments = new HashMap<String, String>();
private String selectedDepartment;


    departments.put("department1",
            "department1");
    departments.put("department2",
            "department2");
    departments.put("department3", "department3");
departments.put("department4", "department4");
departments.put("department5", "department5");





//getters setters...


}

and contents of selectonemenu is not department1,department2,department3,department4,department5 respectively.It is not sorted.

like image 392
mft Avatar asked May 12 '13 10:05

mft


2 Answers

It's not sorted because HashMap doesn't have a predictable iteration order. Try LinkedHashMap instead.

like image 159
dratewka Avatar answered Sep 30 '22 04:09

dratewka


with LinkedHashMap it is ordered not sorted i.e you get it in order which you put the items, but if you use TreeMap then it is sorteted alphabetically

like image 37
suatCoskun Avatar answered Sep 30 '22 02:09

suatCoskun