A List of SelectItems that are often used to set choices. You may want to sort by using the value obtained from the database. For such a case.
--Environment - CentOS Linux release 7.8.2003 (Core) - openjdk version "11.0.7" 2020-04-14 LTS - JSF 2.3.9
For example, when there is a List of SelectItem like this
value | label |
---|---|
1 | Dog |
3 | Monkey |
0 | Bear |
2 | Cat |
/**List of SelectItems. */
@Getter
private List<SelectItem> items;
/**Set a list of SelectItems. */
private void setItems() {
this.items = new ArrayList<SelectItem>();
this.items.add(new SelectItem(1, "Dog"));
this.items.add(new SelectItem(0, "Bear"));
this.items.add(new SelectItem(3, "Monkey"));
this.items.add(new SelectItem(2, "Cat"));
}
Reference: [Collections (Java Platform SE 8)](https://docs.oracle.com/javase/jp/8/docs/api/java/util/Collections.html#sort-java.util.List-java. util.Comparator-)
value | label |
---|---|
0 | Bear |
1 | Dog |
2 | Cat |
3 | Monkey |
Collections.sort(this.items, new Comparator<SelectItem>() {
@Override
public int compare(SelectItem item1, SelectItem item2) {
if (item1 != null && item2 != null) {
if (item1.getValue() != null && item2.getValue() != null) {
return item1.getValue().toString().compareTo(item2.getValue().toString());
}
}
return 0;
}
});
SelectItem minItem = Collections.min(this.items, new Comparator<SelectItem>() {
//The content is the same as sorting
});
var min = Integer.valueOf(minItem.getValue().toString());
Reference: Comparator (Java Platform SE 8)
value | label |
---|---|
3 | Monkey |
2 | Cat |
1 | Dog |
0 | Bear |
//I just reversed item1 and item2 in the ascending return
Collections.sort(this.items, new Comparator<SelectItem>() {
@Override
public int compare(SelectItem item1, SelectItem item2) {
if (item1 != null && item2 != null) {
if (item1.getValue() != null && item2.getValue() != null) {
return item2.getValue().toString().compareTo(item1.getValue().toString());
}
}
return 0;
}
});
Reference: Sort the list of hiragana and alphabets --Qiita
value | label |
---|---|
1 | Dog |
0 | Bear |
3 | Monkey |
2 | Cat |
Comparator.How to use comparing
this.items = this.items.stream().sorted(Comparator.comparing(SelectItem::getLabel)).collect(Collectors.toList());
Collator.How to use getInstance
Collections.sort(this.items, new Comparator<SelectItem>() {
@Override
public int compare(SelectItem item1, SelectItem item2) {
if (item1 != null && item2 != null) {
if (item1.getLabel() != null && item2.getLabel() != null) {
return Collator.getInstance(Locale.JAPANESE).compare(item1.getLabel(), item2.getLabel());
}
}
return 0;
}
});
value | label |
---|---|
2 | Cat |
3 | Monkey |
0 | Bear |
1 | Dog |
Comparator.How to use comparing
// reversed()In descending order
this.items = this.items.stream().sorted(Comparator.comparing(SelectItem::getLabel).reversed()).collect(Collectors.toList());
Collator.How to use getInstance
//I just reversed item1 and item2 in the ascending return
Collections.sort(this.items, new Comparator<SelectItem>() {
@Override
public int compare(SelectItem item1, SelectItem item2) {
if (item1 != null && item2 != null) {
if (item1.getLabel() != null && item2.getLabel() != null) {
return Collator.getInstance(Locale.JAPANESE).compare(item2.getLabel(), item1.getLabel());
}
}
return 0;
}
});
I found a useful thing called Comparator, so I tried to use it immediately and failed.
A compilation error occurs around Comparator.comparing (SelectItem :: getValue)
.
Since the value of SelectItem is Object, can't it be used as a sort key?
this.items = this.items.stream().sorted(Comparator.comparing(SelectItem::getValue)).collect(Collectors.toList());
Recommended Posts