The Coffee Machine

Java & Eclipse tips blog

  • Categories

  • Archives

  •  

    September 2008
    M T W T F S S
    « Aug    
    1234567
    891011121314
    15161718192021
    22232425262728
    2930  

Sort a list of elements with the Comparable interface

Posted by ObLiB on September 11, 2008

JavaSometimes you’ve got to sort a list. Instead of doing our own sort method, you can use the Collections.sort(List list) method. To be sorted, your list elements must be “Comparable” (Like int, String, Date etc…). To do this, the only things to do are to implements the Comparable interface and define the public int compareTo(Object object) method in your element class.

Let’s practice a while to see how it works. We will define a house with a surface and its construction’s year:

public class House implements Comparable<House> {
private int sqFt;
private int constructionYear;

public int getSqFt() {
return sqFt;
}
public void setSqFt(int sqFt) {
this.sqFt = sqFt;
}

public int getContructionYear() {
return constructionYear;
}
public void setContructionYear(int constructionYear) {
this.constructionYear = constructionYear;
}

public House(int sqFt, int contructionYear) {
setSqFt(sqFt);
setContructionYear(contructionYear);
}

public int compareTo(House house) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;

if(this == house) return EQUAL;

// bigger if surface is bigger
if(getSqFt() < house.getSqFt()) return BEFORE;
if(getSqFt() > house.getSqFt()) return AFTER;

// bigger if newer
if(getContructionYear() < house.getContructionYear()) return BEFORE;
if(getContructionYear() > house.getContructionYear()) return AFTER;

// else equal
return EQUAL;
}

public String toString() {
return getSqFt()+" sq ft, build in "+getContructionYear();
}
}

Our House class implements the Comparable<T> interface where T defines the type of Object which the House will be compare to. In our example we’ll compare a House to another, so it’s Comparable<House>.

Next we have defined the compareTo(<T> object) Comparable method. This method will be used to sort our houses. We decide to order them in this way:

  • by surface (if house A is smaller than house B, house A is less than house B)
  • by construction year (if two houses have the same surface, the newest is the biggest)

Now, we will define a list of houses and try to sort them in ascending order and in descending order:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
public static void showHouseList(List<House> houseList) {
for(House house : houseList)
System.out.println(house);
}

public static void main(String[] args) {
List<House> houseList = new ArrayList<House>();

houseList.add(new House(2000, 1976));
houseList.add(new House(20000, 1999));
houseList.add(new House(5000, 1990));
houseList.add(new House(2900, 1982));
houseList.add(new House(20000, 1998));
houseList.add(new House(10000, 1979));
houseList.add(new House(10000, 2005));
houseList.add(new House(15000, 1995));
houseList.add(new House(3000, 1980));
houseList.add(new House(5000, 1999));
houseList.add(new House(6000, 1968));

System.out.println("- Mixing the list...");
// Mixing the house list
Collections.shuffle(houseList);
// Checking the state of our list
showHouseList(houseList);

System.out.println("\n- After sorting in ascending order...");
// Sorting our house in ascending order
Collections.sort(houseList);
// Checking that the sort action works
showHouseList(houseList);

System.out.println("\n- Re mixing the list...");
// Re-Mixing the house list
Collections.shuffle(houseList);
// Checking the state of our list
showHouseList(houseList);

System.out.println("\n- After sorting in descending order...");
// Sorting our house in descending order
Collections.sort(houseList, Collections.reverseOrder());
// Checking that the sort action works
showHouseList(houseList);
}
}

Here is the output:

- Mixing the list...
15000 sq ft, build in 1995
2900 sq ft, build in 1982
5000 sq ft, build in 1999
6000 sq ft, build in 1968
10000 sq ft, build in 2005
10000 sq ft, build in 1979
20000 sq ft, build in 1999
2000 sq ft, build in 1976
20000 sq ft, build in 1998
5000 sq ft, build in 1990
3000 sq ft, build in 1980

- After sorting in ascending order...
2000 sq ft, build in 1976
2900 sq ft, build in 1982
3000 sq ft, build in 1980
5000 sq ft, build in 1990
5000 sq ft, build in 1999
6000 sq ft, build in 1968
10000 sq ft, build in 1979
10000 sq ft, build in 2005
15000 sq ft, build in 1995
20000 sq ft, build in 1998
20000 sq ft, build in 1999

- Re mixing the list...
3000 sq ft, build in 1980
15000 sq ft, build in 1995
10000 sq ft, build in 2005
5000 sq ft, build in 1990
2900 sq ft, build in 1982
2000 sq ft, build in 1976
20000 sq ft, build in 1998
10000 sq ft, build in 1979
6000 sq ft, build in 1968

5000 sq ft, build in 1999
20000 sq ft, build in 1999

- After sorting in descending order...
20000 sq ft, build in 1999
20000 sq ft, build in 1998
15000 sq ft, build in 1995
10000 sq ft, build in 2005
10000 sq ft, build in 1979
6000 sq ft, build in 1968
5000 sq ft, build in 1999
5000 sq ft, build in 1990
3000 sq ft, build in 1980
2900 sq ft, build in 1982
2000 sq ft, build in 1976

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>