Lists and Arrays of objects that implement Comparable interface can be sorted automatically by Collections.sort and Arrays.sort respectively.
Implementation Comparable is easy. You just have to implement compareTo(Object o). It should return negative integer, zero, or a positive integer as this object is less than, equal to,or greater than the specified object.
Here goes an example:
public void testListSort(){
Calendar cal = Calendar.getInstance();
cal.set(2010, 10, 1);
License msWindows7 = new License("Windows", "7",new Date(cal.getTimeInMillis()));
cal.clear();
cal.set(2010, 11, 10);
License msWindowsVista = new License("Windows", "Vista", new Date(cal.getTimeInMillis()));
cal.clear();
cal.set(2010, 11, 5);
License lotusNotes = new License("Notes", "1.1", new Date(cal.getTimeInMillis()));
List list = new ArrayList(3);
list.add(lotusNotes);
list.add(msWindowsVista);
list.add(msWindows7);
Collections.sort(list);
for(License l : list){
System.out.println(l);
}
}
private class License implements Comparable{
String id;
String version;
Date expiryDate;
public License(String id, String version,
Date expiryDate) {
this.id = id;
this.version = version;
this.expiryDate = expiryDate;
}
@Override
public int compareTo(License license) {
// Returns a negative integer, zero, or a positive integer as this object is less than,
// equal to,or greater than the specified object.
if(this.expiryDate.after(license.expiryDate)){
return 1;
}
else{
return 0;
}
}
@Override
public String toString(){
return "id: " + id + "\n" +
"version: " + version + "\n" +
"expiryDate: " + expiryDate + "\n\n";
}
}
Your comments are welcome as always :)
Implementation Comparable is easy. You just have to implement compareTo(Object o). It should return negative integer, zero, or a positive integer as this object is less than, equal to,or greater than the specified object.
Here goes an example:
public void testListSort(){
Calendar cal = Calendar.getInstance();
cal.set(2010, 10, 1);
License msWindows7 = new License("Windows", "7",new Date(cal.getTimeInMillis()));
cal.clear();
cal.set(2010, 11, 10);
License msWindowsVista = new License("Windows", "Vista", new Date(cal.getTimeInMillis()));
cal.clear();
cal.set(2010, 11, 5);
License lotusNotes = new License("Notes", "1.1", new Date(cal.getTimeInMillis()));
List
list.add(lotusNotes);
list.add(msWindowsVista);
list.add(msWindows7);
Collections.sort(list);
for(License l : list){
System.out.println(l);
}
}
private class License implements Comparable
String id;
String version;
Date expiryDate;
public License(String id, String version,
Date expiryDate) {
this.id = id;
this.version = version;
this.expiryDate = expiryDate;
}
@Override
public int compareTo(License license) {
// Returns a negative integer, zero, or a positive integer as this object is less than,
// equal to,or greater than the specified object.
if(this.expiryDate.after(license.expiryDate)){
return 1;
}
else{
return 0;
}
}
@Override
public String toString(){
return "id: " + id + "\n" +
"version: " + version + "\n" +
"expiryDate: " + expiryDate + "\n\n";
}
}
Your comments are welcome as always :)
Comments