Drill

ProblemsJava › data

The order that sorts

harddataSortingArraysJava

A report lists items by rank without disturbing the source rows: instead of the values, it wants their positions.

sortedIndexOrder(values: list<int>) → list<int>

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

List<Integer> sortedIndexOrder(List<Integer> values) {
    
}

Worked examples

CallResult
sortedIndexOrder(Main.<Integer>ls(40, 10, 30, 10))Main.<Integer>ls(1, 3, 2, 0)
sortedIndexOrder(Main.<Integer>ls(20, 10, 30, 10))Main.<Integer>ls(1, 3, 0, 2)
sortedIndexOrder(Main.<Integer>ls(1, 2, 3))Main.<Integer>ls(0, 1, 2)
sortedIndexOrder(Main.<Integer>ls(3, 2, 1))Main.<Integer>ls(2, 1, 0)

Hint

Sort a list of the indices with a comparator that looks the values up, falling back to the index itself on a tie.

Reference solution in Java
List<Integer> sortedIndexOrder(List<Integer> values) {
    List<Integer> idx = new ArrayList<>();
    for (int i = 0; i < values.size(); i++) idx.add(i);
    Collections.sort(idx, (a, b) -> {
        int d = values.get(a) - values.get(b);
        return d != 0 ? d : a - b;
    });
    return idx;
}

The same problem in another language

More data problems in Java