I need to use map collection and I decided to use treemap (because of sorted keys). However, treemap sorts alphabetically my list from "A -> Z" and then "a -> z". For example input = zgaZGA output = AGZagz
Do anyone have any idea how to sort it beginning from lower case? (I mean to have output = agzAGZ)
I'm writing in Java language (Java SE8) Thanks for any help!
You can provide your own custom Comparator
:
public class LowerCaseFirstComaparor implements Comparator<Character> {
@Override
public int compare (Character c1, Character c2) {
if (Character.isLowerCase(c1)) {
if (Character.isLowerCase(c2)) {
return c1.compareTo(c2);
} else {
return -1;
}
} else if (Character.isLowerCase(c2)) {
return 1;
} else {
return c1.compareTo(c2);
}
}
And then use it when creating the TreeMap
:
Map<Character, SomeClass> map = new TreeMap<>(new LowerCaseFirstComaparor());