Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort f:selectItems list based on labels

Tags:

java

jsf

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-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?

like image 991
shaleen Avatar asked Dec 04 '22 23:12

shaleen


2 Answers

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());
}
like image 92
JB Nizet Avatar answered Dec 30 '22 00:12

JB Nizet


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
like image 29
mre Avatar answered Dec 30 '22 01:12

mre