sort List of bean object in java alphabetically in ascending order

时间:2016-04-04 16:46:33

标签: java comparator comparable

Below is the bean class:

public class bean  {
    private String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }



    @Override
    public String toString() {
        return "bean [Name=" + Name + "]";
    }
}

Below is my java class where i am trying to sort the list of beans in ascending order(i mean alphabetical order):

 public class Test{
    public static void main(String[] args) {
        bean b1 = new bean();
        bean b2 = new bean();
        bean b3 = new bean();
        List<bean> beanList = new ArrayList<bean>();
        b1.setName("b");
        b2.setName("a");
        b3.setName("Z");
        beanList.add(b1);
        beanList.add(b2);
        beanList.add(b3);

    }
}

i am unable to achieve the sorting of the bean value in alphabetical order like a,b,z. can anyone please suggest

2 个答案:

答案 0 :(得分:3)

You are sorting 2 times and that is more that what you need

1st Test doesnt need to implement comparable...

public class Test  {
    public static void main(String[] args) { 
        Bean b1 = new Bean();
        Bean b2 = new Bean();
        Bean b3 = new Bean();
        List<Bean> beanList = new ArrayList<Bean>();
        b1.setName("b");
        b2.setName("a");
        b3.setName("Z");
        beanList.add(b1);
        beanList.add(b2);
        beanList.add(b3);

        Collections.sort(beanList);
        System.out.println("finally " + beanList);
    }
}

and the bean class should compare the strings since that is the sort criteria

public class Bean implements Comparable<Bean> {
    private String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @Override
    public String toString() {
        return "bean [Name=" + Name + "]";
    }

    @Override
    public int compareTo(Bean o) {
        return o.getName().compareTo(this.getName());
    }
}

edit:

you can for sure add more than one field to the bean class, example: lastname or you can use an anonymous comparator and sort with yoor own criteria

Example with last name:

public class Bean implements Comparable<Bean> {
    private String name;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Bean [name=" + name + ", lastName=" + lastName + "]";
    }

    @Override
    public int compareTo(Bean o) {
        return this.getName().compareTo(o.getName());
    }
}

答案 1 :(得分:2)

The first implementation of sort which uses the inline definition of comparator is correct.

You would find that in the the sorted beans occur in the order of "Z","a" and "b".

Capital Z is being compared with small letters a and b. Hence, the sorted list is in above order

If you wish that the comparison should be case insensitive, you can use tolowercase on both strings before comparison.

Refer foll. code snippet:

Collections.sort(beanList, new Comparator<bean>() {
                public int compare(final bean object1, final bean object2) {
                    return object1.getName().toLowerCase().compareTo(object2.getName().toLowerCase());
                }
            });

You can also use compareToIgnoreCase() method as follows:

Collections.sort(beanList, new Comparator<bean>() {
                public int compare(final bean object1, final bean object2) {
                    return object1.getName().compareToIgnoreCase(object2.getName());
                }
            });

Alternatively, you can use the second method(Collections.sort()), where you do not mention explicitly the comparator, while invoking sort on the Collection. This would be possible as the bean class implements the comparable interface.

For implementing the comparable interface, compareTo method would have to implemented as follows:

@Override
public int compareTo(bean o) {
    return this.Name.compareToIgnoreCase(o.Name);
}