I am trying to sort a list against a custom order, I have already followed the stackOverflow link. The custom order must be as followed : "ST, SIT, JDC" which has been done with an array list in customOrder. and a data comming from database must be shown in a list following the above order, but the problem is that this is only work if the data from the database match exactly to this list, like if I have an exact word of "ST" or "SIT" then it do the job. but the data that I am getting from the database looks like this :
ST1,ST2,ST3, SIT1,SIT2, JDC Release, JDC Stop, JDC 10
this code doesnt work for my data, there is no error in the result, but it doesnt sort the data at all.
here is the code :
List<Environment> environments = environmentDAO.getAll();
final List<String> customOrder = Arrays.asList("ST", "SIT", "JDC");
Collections.sort(environments, new Comparator<Environment>() {
@Override
public int compare(final Environment o1, final Environment o2) {
Integer firstValue = Integer.valueOf(customOrder.indexOf(o1.getAcronym()));
Integer secondValue = Integer.valueOf(customOrder.indexOf(o2.getAcronym()));
int comparedTo = firstValue.compareTo(secondValue);
return comparedTo;
}
});