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.
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.
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
indexOfinside 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.
- '
indexOfworks 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.
Related reading
- javac option to compile all java files under a given directory recursively
- Javascript algorithm to find elements in array that are not in another array
- Javascript Array.sort implementation?
- JavaScript Calculate the nth root of a number
- Java Stream API - Best way to transform a list map or forEach?
- Java String array is there a size of method?
- Java splitting a comma-separated string but ignoring commas in quotes
- Java Spring Boot - Change the Port of Actuator Health Endpoint to a Custom Port
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

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 courseData 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.