So, ganz passts doch noch nicht ich bekomme beim value "null". Weiß das jemand wieso?
[code=Java]
package mapsorttest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class SortTest implements Comparator<String> {
public SortTest() {
final Map<String, String> map1 = new HashMap<String, String>();
TreeMap tm1= new TreeMap();
tm1.put("15.08.2012", 1);
tm1.put("25.03.2012", 2);
tm1.put("06.05.2012", 3);
tm1.put("31.01.2012", 4);
tm1.put("30.01.2012", 5);
TreeMap tm2= new TreeMap(this);
tm2.putAll(tm1);
System.out.println("tm2: " + tm2);
for(final Iterator<String> keys = tm2.keySet().iterator(); keys.hasNext();) {
final String competitionDateValue = keys.next();
System.out.println(competitionDateValue + "," + tm2.get(competitionDateValue));
}
}
@Override
public int compare(final String s1, final String s2) {
if (s1 == null && s2 == null)
return 0;
if (s1 == null)
return 1;
if (s2 == null)
return -1;
final SimpleDateFormat sdfToDate = new SimpleDateFormat("dd.MM.yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = sdfToDate.parse(s1);
date2 = sdfToDate.parse(s2);
}
catch (ParseException ex) {
System.out.println("Exception in class MapComparator in method compare: " + ex);
}
final int value = date1.after(date2) ? 1 : -1;
return value;
}
public static void main(String[] args) throws ParseException {
new SortTest();
}
}
[/code]