@Override
    public Tensor evaluate(EvaluationContext<NAMETYPE> context) {
        Tensor input = argument().evaluate(context);
        List<Double> values = new ArrayList<>();
        for (Iterator<Tensor.Cell> i = input.cellIterator(); i.hasNext(); ) {
            values.add(i.next().getValue());
        }
        List<Integer> indexes = new ArrayList<>();
        for (int i = 0; i < values.size(); i++) {
            indexes.add(i);
        }
        if (order == Order.MAX) {
            indexes.sort((a, b) -> nanAwareCompare(values.get(b), values.get(a)));
        } else {
            indexes.sort((a, b) -> nanAwareCompare(values.get(a), values.get(b)));
        }
        List<Integer> ranks = new ArrayList<>(indexes.size());
        for (int i = 0; i < indexes.size(); i++) {
            ranks.add(0);
        }
        for (int i = 0; i < indexes.size(); i++) {
            ranks.set(indexes.get(i), i);
        }
        Tensor.Builder builder = Tensor.Builder.of(outputType(input.type()));
        Iterator<Tensor.Cell> cells = input.cellIterator();
        for (int i = 0; cells.hasNext(); i++) {
            Tensor.Cell cell = cells.next();
            int rank = ranks.get(i);
            builder.cell(cell.getKey(), (double) rank);
        }
        return builder.build();
    }
The <i>cell_order</i> tensor function produces a tensor with the rank of each
value in the original tensor according to the given ordering

@author arnej
vespajlib/src/main/java/com/yahoo/tensor/functions/CellOrder.java