Java
Sorting
Arrays
indexOf
Programming Techniques

Java Sorting an array based on another array with indexOf method

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

If you want to sort one array according to the custom order defined by another array, using indexOf inside the comparator can work for small inputs but scales poorly. The better pattern is to build a rank map once and let the comparator look up the desired order in constant time.

The indexOf Idea Works but Is Repeatedly Expensive

A naive approach converts the reference order to a list and uses indexOf during each comparison. That can become expensive because each comparison may scan the reference list again.

The algorithm still works conceptually, but it is not the best implementation strategy when the arrays are nontrivial in size.

Build a Rank Map Instead

A faster solution is to precompute the desired rank of each value.

java
1import java.util.*;
2
3String[] order = {"banana", "apple", "orange"};
4String[] values = {"orange", "banana", "apple"};
5
6Map<String, Integer> rank = new HashMap<>();
7for (int i = 0; i < order.length; i++) {
8    rank.put(order[i], i);
9}
10
11Arrays.sort(values, Comparator.comparingInt(v -> rank.getOrDefault(v, Integer.MAX_VALUE)));
12System.out.println(Arrays.toString(values));

This keeps the sorting logic clear and avoids repeated indexOf scans.

Decide What Happens to Missing Values

An important design choice is what to do with elements that are not present in the reference array. In the example above, they are pushed to the end by using Integer.MAX_VALUE as the fallback rank.

That policy should be explicit. Otherwise, the sort order for unexpected values becomes ambiguous.

Common Pitfalls

  • Using indexOf inside the comparator and paying repeated linear-search cost.
  • Forgetting to define how values missing from the reference array should be ordered.
  • Building the rank logic correctly but then sorting a primitive array that needs a different comparator strategy.
  • Treating custom-order sorting as though it were ordinary alphabetical sorting.
  • Ignoring the possibility of duplicate or inconsistent values in the reference order.

Summary

  • Sorting by another array means sorting by a custom rank, not by natural order.
  • 'indexOf works conceptually but is often inefficient inside a comparator.'
  • A precomputed rank map is the standard efficient solution.
  • Decide explicitly where missing values belong.
  • The comparator should express the external order directly and cheaply.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms