0

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

Sangeetha cg
  • 81
  • 5
  • 14
  • You're calling `sort` twice. Why? – NilsH Apr 04 '16 at 16:50
  • was trying to find the various ways where i can achieve. at the end the functionality was to achieve the alphabetical ordering, in which i was failing – Sangeetha cg Apr 04 '16 at 16:52
  • Look your implementation of compareTo(): it always returns 0. So all the objects are considered equal by your comaprison method. And please, respect the Java naming conventions: classes start with an uppercase letter, so bean -> Bean. Variables start with a lowercase letter, so Name -> name. – JB Nizet Apr 04 '16 at 16:57
  • yeah sure will follow. public int compareTo(bean b1, bean b2) { return b1.getName().compareTo(b2.getName()); } i have added this code still it does not work – Sangeetha cg Apr 04 '16 at 17:00
  • This is not the method that implement Comparable. You should remove it: it's useless. Read the javadoc of Comparable. Does it have a method with two arguments? – JB Nizet Apr 04 '16 at 17:12

2 Answers2

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());
    }
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
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);
}