2

I have an enum:

public static enum States {

    SEL("Selangor"),
    WP("Wilayah Persekutuan"),      
    KEL("Kelantan"),
    LAB("Labuan"),
    PER("Perlis");  

    private String value;

    States(String val) {
        this.value = val ;
    }

    public String getValue() {
        return value;
    }       
}

The values are displayed on a Thymeleaf template by:

<div class="col-xs-6 padding-sides-none">
  <div class="form-group col-xs-12 padding-sides-none">
    <label class="col-xs-2 padding-sides-none" style="margin-left:15px">State</label>
    <div class="col-xs-7 padding-sides-none">
      <select class="form-control selectpicker" data-live-search="true"
              style="width:300px;margin-left: 50px;">
        <option value="" selected disabled hidden>Please select a state</option>
        <option th:each="state : ${T(com.workspez.model.Accountant.States).values()}"
                th:value="${state}" th:text="${state.getValue()}">
        </option>
      </select>
    </div>
  </div>
</div>

I would like to display the values in alphabetical order. is it possible? One way is to manually arrange the enum but is there any way to sort the enum list? Thanks.

Mahozad
  • 18,032
  • 13
  • 118
  • 133

2 Answers2

2

You can use comparator inside States enum to compare and sort enum values like this:

public enum States {
    SEL("Selangor"),
    WP("Wilayah Persekutuan"),      
    KEL("Kelantan"),
    LAB("Labuan"),
    PER("Perlis");  

    private String value;       
    States(String val){
        this.value = val ;
    }       
    public String getValue(){
        return value;
    }

    public static States[] getSortedValue(){
        States[] values = States.values();
        Arrays.sort(values,(s1,s2)->s1.getValue().compareTo(s2.getValue()));
        return values;
    }
}

In your thymeleaf HTML just call getSortedValuemethod to get values in ascending order like this:

<option th:each="state : ${T(com.crud.example.enums.States).getSortedValue()}"
th:value="${state}" 
th:text="${state.getValue()}">
</option>
Ajit Soman
  • 3,926
  • 3
  • 22
  • 41
  • ${T(com.workspez.model.Accountant.States.getSortedValue()).values()} –  Feb 17 '18 at 16:06
  • I did '${T(com.workspez.model.Accountant.States.getSortedValue()).values()}' and i get org.springframework.expression.spel.SpelParseException: EL1043E:(pos 53): Unexpected token. Expected 'rparen())' but was 'lparen(()' error –  Feb 17 '18 at 16:08
  • how would get selected value? i tried below and combo box wont get populated –  Feb 18 '18 at 17:24
0

how would one get the selected value in combobox? I did :

<select th:field="*{currentAccountant.state}" class="form-control selectpicker"  data-live-search="true" style="width:300px;margin-left: 50px;"  >
                                      <option value="" selected disabled hidden>Please select a state</option>
                                     <option th:each="state : ${T(com.model.Accountant.States).getSortedValue()}"
                                        th:value="${state}" 
                                        th:text="${state.getValue()}">
                                    </option>
                                </select>

The values no longer get populated.I removed th:field="*{currentAccountant.state}" then it gets populated