开发者

Sort f:selectItems list based on labels

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>

Here 1, 2, 3 are the values for selectItem list and '100开发者_StackOverflow中文版-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?


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


Here's an example program I quickly wrote that uses a custom comparator:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.JLabel;

public class StrangeSort
{
    static List<JLabel> listOfLabels = new ArrayList<JLabel>();

    @SuppressWarnings("unchecked")
    public static void main(String[] args)
    {
        listOfLabels.add(new JLabel("100-500"));
        listOfLabels.add(new JLabel("1000-1500"));
        listOfLabels.add(new JLabel("500-1000"));

        System.out.println("Before:");
        for(JLabel label: listOfLabels)
        {
            System.out.println(label.getText());
        }

        Collections.sort(listOfLabels, new LabelComparator());

        System.out.println("After:");
        for(JLabel label: listOfLabels)
        {
            System.out.println(label.getText());
        }

    }

    static class LabelComparator implements Comparator
    {
        public int compare(Object obj1, Object obj2)
        {
            // Get labels
            JLabel label1 = (JLabel) obj1;
            JLabel label2 = (JLabel) obj2;

            // Get text
            String[] label1Text = label1.getText().split("-");
            String[] label2Text = label2.getText().split("-");

            // Convert to integers
            int label1Value = Integer.parseInt(label1Text[0]);
            int label2Value = Integer.parseInt(label2Text[0]);

            // Compare values
            if (label1Value > label2Value)
            {
                return 1;
            }
            else if (label1Value < label2Value)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }
}

Output:

Before:
100-500
1000-1500
500-1000
After:
100-500
500-1000
1000-1500


I use Commons BeanUtils and do it with 1 line of code....

// sort by label
Collections.sort(selectItems, new BeanComparator<>("label"));

OR...

// sort by value
Collections.sort(selectItems, new BeanComparator<>("value"));


Implement a comparator.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html


Sort by Label

Take a look at the code posted by "JB Nizet". It sorts by value. If you change
return s1.getValue().compareTo(s2.getValue());
to the following:
return s1.getLabel().compareTo(s2.getLabel());
It will sort by label.

Sort Label Numerically

The above will sort by string value. Which means that 1 is less than 5 (i.e. 1000 < 500). The next step is to convert the label to a number and compare it. For this use a variation of the following:


int value1 = Integer.parseInt(s1.getLabel());
int value2 = Integer.parseInt(s2.getLabel());
return value1 - value2;


Thanks you very much for your help. I am getting the desired result. Please find below the code for your reference.

  /**
   * This is a custom comparator used for numeric sorting based on labels 
  */
  static class LabelComparator implements Comparator {
        public int compare(Object obj1, Object obj2) {
              // Get labels
              SelectItem label1 = (SelectItem) obj1;
              SelectItem label2 = (SelectItem) obj2;
              // Get text
              String[] label1Text = label1.getLabel().split("-");
              String[] label2Text = label2.getLabel().split("-");
              // Convert to integers
              int label1Value = Integer.parseInt(label1Text[0]);
              int label2Value = Integer.parseInt(label2Text[0]);
              // Compare values
              if (label1Value > label2Value) {
                    return 1;
              } else if (label1Value < label2Value) {
                    return -1;
              } else {
                    return 0;
              }
        }
  }

  /**
   * Method to populate values  
  */
  public void init() {
        List rangeList = (List) CodeManagerUtil
                    .getValue("Staff Range");
        Collections.sort(rangeList, new LabelComparator());
        if (rangeList != null) {
              setRangeList(rangeList );
        }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜