Fidel
Fidel

Reputation: 7397

Does this sort order have a name?

Does the following sort order have a name? Given an index, sort the list in order of proximity to that 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 the list [1,2,3,4,5,6,7,8,9,10] and a centerIndex of 7, the sorted list would be: [7,8,6,9,5,10,4,3,2,1]

Upvotes: 4

Views: 93

Answers (1)

Adrien Piquerez
Adrien Piquerez

Reputation: 1044

"Sort by distance" is an entirely appropriate name since your compare method is a metric or distance function. See wikipedia

Upvotes: 1

Related Questions