I have a TreeMap
which needs to be sorted based on keys. Which is default property of TreeMap
. But in my case I am not able to figure out the Comparator. Following is my Code.
public class Test {
public static void main(String[] args) {
Map<String, String> aMap = new TreeMap<String, String>(new MyComp());
aMap.put("02_file.cql", "test");
aMap.put("01.cql", "test");
aMap.put("04.cql", "test");
aMap.put("3_file.cql", "test");
aMap.put("11_file.cql", "test");
aMap.put("10_file.cql", "test");
aMap.put("0_file.cql", "test");
aMap.put("100_file.cql", "test");
Set<Map.Entry<String,String>> set = aMap.entrySet();
for(Map.Entry<String,String> e : set){
System.out.println(e.getKey() + ":" + e.getValue());
}
}
}
class MyComp implements Comparator<String> {
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
}
The output is coming as :
01.cql:test
02_file.cql:test
04.cql:test
0_file.cql:test
100_file.cql:test
10_file.cql:test
11_file.cql:test
3_file.cql:test
Which is not my expected result. I am expecting result like:
0_file.cql:test
01.cql:test
02_file.cql:test
3_file.cql:test
04.cql:test
10_file.cql:test
11_file.cql:test
100_file.cql:test
Which is same as like what NameFileComparator.NAME_COMPARATOR
org.apache.commons.io.comparator.NameFileComparator;
Any suggestion?