Does the following sort order have a name? Based on the index, sort the list in order of proximity to this index.
Collections.sort(items, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int distanceA = (int)Math.abs(centerIndex - items.indexOf(o1));
int distanceB = (int)Math.abs(centerIndex - items.indexOf(o2));
int result = distanceA - distanceB;
return result;
}
}
So, for a list [1,2,3,4,5,6,7,8,9,10]and centerIndex of 7, the sorted list would be:[7,8,6,9,5,10,4,3,2,1]
Fidel source
share