How to sort JAVA collection elements

Probably you've worked with List in a moment of your JAVA developer life, and we can say that using a List instead an array is more beautiful because the list grows automatically, you can add elements easily (only objects, but if you want to add primitives use wrapper classes) and so on. But, How to sort the elements of a list?
Suppose that you have a list of employees. Those employees have the next data:
complete name age sex position
So, you have your JAVA bean Employeee, like this: (Continue reading the post)
public class Employee {
private String completeName;
private int age;
private boolean sex;
private String position;
@Override
public String toString()
{
return getCompleteName() + ", " + getAge() + " years, " +
(isSex()?"male, ":"female, ") + getPosition();
}
public Employee(String completeName, int age, boolean sex, String position) {
setCompleteName(completeName);
setAge(age);
setSex(sex);
setPosition(position);
}
public String getCompleteName() {
return completeName;
}
public void setCompleteName(String completeName) {
this.completeName = completeName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
And you want to order by their complete name. How to do that? First, we have some code to fill our list with some employees (this in main(String[] args) ):
List<Employee> ourEmployeesList = new ArrayList<Employee>();
ourEmployeesList.add(new Employee("John Connor", 34, true, "Fighter against Terminators"));
ourEmployeesList.add(new Employee("Oscar Calderon", 22, true, "Developer"));
ourEmployeesList.add(new Employee("Scarlett Johansson", 28, false, "Actress"));
ourEmployeesList.add(new Employee("Zandrox Ubliq", 67, false, "I dont know"));
//Order our list?
//Iterate through the list
for(Employee tmpEmp : ourEmployeesList)
{
System.out.println(tmpEmp.toString());
}
Because ArrayList orders the elements by index you will get the next output:
John Connor, 34 years, male, Fighter against Terminators Oscar Calderon, 22 years, male, Developer Scarlett Johansson, 28 years, female, Actress Zandrox Ubliq, 67 years, female, I dont know
To order our famous people list, we have to modify a little bit our Employee class implementing the Comparable interface. Comparable contract requires to implement compareTo() method, a method used by the class that we will use to order the list. this class is Collections, that contents static methods to work with collections. The key method here is sort(), a method that receives a collection and order it based in it's compareTo() method. In the case of classes like String, this method is implemented so we don't need to do anything else, but with custom objects like Employee we need to implement this method. This method returns an int an receives an object, and it compares the parameter against the method of the instance that is calling compareTo(). You can return the next values:
negative If thisObject < anotherObject zero If thisObject == anotherObject positive If thisObject > anotherObject
In this case, we want to order the list by employee name, so we are going to use the compareTo() method
of String class, and our compareTo() method looks like this:
public int compareTo(Employee o) {
return getCompleteName().compareTo(o.getCompleteName());
}
The code of your Employee class doesn't compile with this method? That's because the original implementation receives a Object as parameter and we declared as Employee, this can be done because in Java 5 Comparable class is Generic, allowing us to specify the kind of object that receives the compareTo() method.
After that, we only need to modify the code where we will order the list. To do that, we just call sort() method of Collection class passing the list as parameter, so finally the code of our main() method looks like this:
List<Employee> ourEmployeesList = new ArrayList<Employee>();
ourEmployeesList.add(new Employee("John Connor", 34, true, "Fighter against Terminators"));
ourEmployeesList.add(new Employee("Oscar Calderon", 22, true, "Developer"));
ourEmployeesList.add(new Employee("Scarlett Johansson", 28, false, "Actress"));
ourEmployeesList.add(new Employee("Zandrox Ubliq", 67, false, "I dont know"));
//Order our list
Collections.sort(ourEmployeesList);
//Iterate through the list
for(Employee tmpEmp : ourEmployeesList)
{
System.out.println(tmpEmp.toString());
}
And, we get the next output:
John Connor, 34 years, male, Fighter against Terminators Oscar Calderon, 22 years, male, Developer Scarlett Johansson, 28 years, female, Actress Zandrox Ubliq, 67 years, female, I dont know
Now, i've ordered my list based on compareTo() method that i have implemented in my Employee class. What about if i want to have avaliable different ways to order my Employee list? Read our next post to know How to do that
. EDITED: READ IT >>HERE<<.
And also, you can download the source code of this post >>HERE<<, it's in a single class with Employee as inner class. Enjoy it.
private String completeName;
private int age;
private boolean sex;
private String position;
public Employee(String completeName, int age, boolean sex, String position) {
setCompleteName(completeName);
setAge(age);
setSex(sex);
setPosition(position);
}
public String getCompleteName() {
return completeName;
}
public void setCompleteName(String completeName) {
this.completeName = completeName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}







