java – 基于标签排序f:selectItems列表
发布时间:2020-12-15 04:54:09 所属栏目:Java 来源:网络整理
导读:我有一个selectItem值和标签列表.数据从数据库中获取,selectItem列表具有以下值: 1,100-5002,1000-15003,500-1000 这里1,2,3是selectItem列表的值,’100-500′,’1000-1500’和’500-1000’分别是标签.如您所见,列表已根据标签进行排序.但我的要求是列表应
我有一个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 有人可以建议一个解决方案? 解决方法
如果您无法修改从DB中获取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()); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |