排序f:基于标签的selectItems列表 [英] Sort f:selectItems list based on labels

查看:88
本文介绍了排序f:基于标签的selectItems列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个selectItem值和标签列表。数据从数据库中获取,selectItem列表具有以下值:

I have a selectItem list of values and labels. Data is fetched from database and the selectItem list has following values:

<1,100-500>
<2,1000-1500>
<3,500-1000>

这里1,2,3是selectItem列表和'100-500'的值,'1000 -1500'和'500-1000'分别是标签。如您所见,列表已根据标签进行排序。但我的要求是列表应显示在下拉列表中,如下所示:

Here 1, 2, 3 are the values for selectItem list and '100-500', '1000-1500' and '500-1000' are labels respectively. As you can see, the list is already sorted based on labels. But my requirement is that the list should be displayed in the dropdown as follows:

100-500
500-1000
1000-1500

有人可以推荐一个解决方案吗?

Can anyone please suggest a solution?

推荐答案

如果你不能修改从DB中获取SelectItem实例的代码,以便按照你的意愿进行排序,那么你必须排序他们自己:

If you can't modify the code which fetches the SelectItem instances from the DB so that the come sorted as you'd like, then you have to sort them yourself :

// 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());
}

这篇关于排序f:基于标签的selectItems列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆