我有一个包含值和标签的 selectItem 列表。数据是从数据库中获取的,selectItem 列表具有以下值:
<1,100-500>
<2,1000-1500>
<3,500-1000>
这里 1、2、3 是 selectItem 列表的值,'100-500'、'1000-1500' 和 '500-1000' 分别是标签。如您所见,列表已根据标签排序。但我的要求是列表应显示在下拉列表中,如下所示:
100-500
500-1000
1000-1500
有人可以提出解决方案吗?
如果您无法修改从数据库获取 SelectItem 实例的代码以便按您想要的方式排序,那么您必须自己对它们进行排序:
// this gets you a list which is not sorted as you would like
List<SelectItem> list = getMasterValues("Staff Range");
// you need to sort the list before displaying it.
// To sort the list, you need a Comparator which will tell the sorting
// algorithm how to order the items
Comparator<SelectItem> comparator = new Comparator<SelectItem>() {
@Override
public int compare(SelectItem s1, SelectItem s2) {
// the items must be compared based on their value (assuming String or Integer value here)
return s1.getValue().compareTo(s2.getValue());
}
};
// now that you have the comparator, sort the list using it :
Collections.sort(list, comparator);
// and now iterate over the list to display it :
for (SelectItem item : list) {
System.out.println("value = " + item.getValue() + "; label = " + item.getLabel());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)