normal sort of list :
private static List VEGETABLES = Arrays.asList("apple", "cocumbers", "blackberry");
Collections.sort(VEGETABLES);
output: apple, blackberry, cocumbers
Reverse sorting:
private static List VEGETABLES = Arrays.asList("apple", "cocumbers", "blackberry");
Collections.sort(VEGETABLES, Collections.reverseOrder());
output: cocumbers, blackberry, apple
with custom comparator:
private class StringComparator implements Comparator {
public int compare(Object o1, Object o2) {
String so1 = (String) o1;
String so2 = (String) o2;
return so1.compareTo(so2);
}
}
private static List VEGETABLES = Arrays.asList("apple", "cocumbers", "blackberry");
Collections.sort(VEGETABLES, new StringComparator());
output: apple, blackberry, cocumbers
Elements sorting:
private class Element implements Comparable{
private String name;
private Double atomicMass;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Element");
sb.append("{name='").append(name).append('\'');
sb.append(", atomicMass=").append(atomicMass);
sb.append('}');
return sb.toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getAtomicMass() {
return atomicMass;
}
public void setAtomicMass(Double atomicMass) {
this.atomicMass = atomicMass;
}
public Element(String name, String mass, double atomicMass) {
this.name = name;
this.atomicMass = atomicMass;
}
public int compareTo(Element o) {
return this.getAtomicMass().compareTo(o.getAtomicMass());
}
}
ArrayListelements = new ArrayList ();
elements.add(new Element("Hydrogen", "H", 1.00794)); // Hydrogen 1.00794 amu Atomic Mass
elements.add(new Element("Iron", "Fe", 55.845));
elements.add(new Element("Lithium", "Li", 6.941));
elements.add(new Element("Lead", "Pb", 207.2));
elements.add(new Element("Magnesium", "Mg", 24.305));
Collections.sort(elements); // Sort by Element
output:
Element{name='Hydrogen', atomicMass=1.00794}
Element{name='Lithium', atomicMass=6.941}
Element{name='Magnesium', atomicMass=24.305}
Element{name='Iron', atomicMass=55.845}
Element{name='Lead', atomicMass=207.2}
Chronological sorting:
SimpleDateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
try {
ArrayListholidays = new ArrayList ();
holidays.add(formatter.parse("May 31, 2010")); // Memorial Day
holidays.add(formatter.parse("July 4, 2010")); // Independence Day
holidays.add(formatter.parse("February 15, 2010")); // Presidents Day
holidays.add(formatter.parse("September 6, 2010")); // Labor Day
holidays.add(formatter.parse("December 24, 2010")); // Thanksgiving Day
holidays.add(formatter.parse("July 5, 2010")); // federal employees extra day off for July 4th
holidays.add(formatter.parse("January 18, 2010")); // Martin Luther King Day
holidays.add(formatter.parse("November 25, 2010")); // federal employees extra day off for Christmas
holidays.add(formatter.parse("October 11, 2010")); // Columbus Day
holidays.add(formatter.parse("December 25, 2010")); // Christmas Day
holidays.add(formatter.parse("January 1, 2010")); // New Year's Day
Collections.sort(holidays); // Native sort for Date is chronological
} catch (ParseException e) {
e.printStackTrace();
}
output: sorted:[Fri Jan 01 00:00:00 CET 2010, Mon Jan 18 00:00:00 CET 2010, Mon Feb 15 00:00:00 CET 2010, Mon May 31 00:00:00 CEST 2010, Sun Jul 04 00:00:00 CEST 2010, Mon Jul 05 00:00:00 CEST 2010, Mon Sep 06 00:00:00 CEST 2010, Mon Oct 11 00:00:00 CEST 2010, Thu Nov 25 00:00:00 CET 2010, Fri Dec 24 00:00:00 CET 2010, Sat Dec 25 00:00:00 CET 2010]
You can view the complete simple class below:
1 commento:
Indeed a good recap. You can also add CASE_INSENSITIVE sorting by using String.CASE_INSENSITIVE_ORDER comparator in Java. By the way I have also shared few tips on using Comparator and Comparable in Java, you may like.
Posta un commento