# Java program to Sort an array according to the order defined by another array

## Sort an array according to the order defined by the other array

Here we will learn about Java program to Sort an array according to the order defined by another array which is discussed over here. Here are given two arrays A [] and B [], sort A in such a way that the relative order among the elements will be the same as those are in B. The elements in array A has to be printed accordingly to the sequence of order of elements specified in array B, the rest of the elements, remaining in array A are printed at the end.

## Example:

Array A [] = {3, 6, 13, 3, 9, 10, 14, 6, 9, 13}

Array B[] = {6, 3, 9, 13, 10}

Output: {6, 6, 3, 3, 9, 9, 13, 13, 10, 14} ## Keypoint

In this section we will learn about basic knowledge which we need to know before coding the above Program. So we must have knowledge of what is an array?

What is an array?
An array is a data structure, it is collection of similar data elements which is stored at contiguous memory locations in which each data element can be accessed directly by only using its index number.

Java is class-based, object-oriented programming language and used for Internet-based applications. Java is a high-level
language.

## Algorithm

step 1- Input both the arrays, arr1 and arr2.

step 2- Span the elements of arr2 from i=1 lenght(arr2).

Step 3- Binary Search to check if arr1 contains the i^th element of Arr2.

Step 4- Print all the elements.

Step 5- Let’s go to step 2 again and again until all the elements of arr2 have been traversed.

Step 6- If there are any remaining element in arr1, print them. ## Code for Java program to Sort an array according to the order defined by another array

```// Java program to sort an array according to the order defined by another array

import java.io.*;
import java.util.Arrays;

class PrepInsta
{

// Binary search
static int first(int arr[], int l, int h,
int x, int n)
{
if (h >= l)
{
int mid = l + (h-l)/2;

if ((mid == 0 || x > array[mid-1]) &&
array[m] == x)
return mid;
if (x > array[m])
return first(arr, (m + 1), h,
x, n);
return first(array, l, (m -1), x, n);
}
return -1;
}

// Sort according to the order defined by array 2
static void sort_according(int array1[], int array2[], int m,
int n)
{

int temp[] = new int[m1], visited[] = new int[m1];
for (int i = 0; i < m1; i++)
{
temp[i] = array1[i];
visited[i] = 0;
}

// Sort elements in temp
Arrays.sort(temp);
int ind = 0;

for (int i = 0; i < n; i++)
{

int f = first(temp, 0, m1-1, array2[i], m);

// If not present, no need to proceed
if (f == -1) continue;

// Copy all occurrences of arr2[i] to arr1[]
for (int j = f; (j < m && temp[j] == arr2[i]);
j++)
{
arr1[ind++] = temp[j];
visited[j] = 1;
}
}

for (int i = 0; i < m1; i++)
if (visited[i] == 0)
arr1[ind++] = temp[i];
}

// Function to print an array
static void print_array(int array[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(array[i] + " ");
System.out.println();
}

public static void main(String args[])
{
int arr1[] = {1, 2, 3, 4, 3, 2, 4, 2, 5};
int arr2[] = {4, 2, 1, 3};
int m = arr1.l;
int n = arr2.l;
System.out.print("The Sorted array : ");
sort_according(array1, array2, m1, n);
print_array(arr1, m1);
}
}```
`Output:- 4, 4, 2, 2, 2, 1, 3, 3, 5`