
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sort an array according to the order defined by another array in C++
In this section we will see another sorting problem. Suppose we have two arrays A1 and A2. We have to sort A1 in such a way that the relative order among the elements will be same as those are in A2. If some elements are not present in A2, then they will be appended after the sorted elements. Suppose A1 and A2 are the following −
A1 = {2, 1, 2, 1, 7, 5, 9, 3, 8, 6, 8} A2 = {2, 1, 8, 3}
After the sorting A1 will be like below −
A1 = {2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9}
To solve this problem, we will create our custom compare method. That method will compare and place the elements in the array. The comparison logic will be like below −
- if num1 and num2 both are in A2, then number with the lower index in A2 will be treated as smaller than the other number
- If either num1 or num2 is present in A2, then that number will be treated as smaller, than the other, which is does not present in A2.
- If both are not present in A2, then natural ordering will be used.
Algorithm
compare(num1, num2): Begin if both num1 and num2 are present in A2, then return index of num1 – index of num2 else if num1 is not in A2, then return -1 else if num2 is not in A1, then return 1 else num1 – num2 End
Example
#include<iostream> #include<algorithm> using namespace std; int size = 5; int A2[5]; //global A2 will be used in compare function int search_index(int key){ int index = 0; for(int i = 0; i < size; i++){ if(A2[i] == key) return i; } return -1; } int compare(const void *num1, const void *num2){ int index1 = search_index(*(int*)num1); int index2 = search_index(*(int*)num2); if (index1 != -1 && index2 != -1) return index1 - index2; else if (index1 != -1) return -1; else if (index2 != -1) return 1; else return (*(int*)num1 - *(int*)num2); } main(){ int data[] = {2, 1, 2, 1, 7, 5, 9, 3, 8, 6, 8}; int n = sizeof(data)/sizeof(data[0]); int a2[] = {2, 1, 8, 3}; int n2 = sizeof(a2)/sizeof(a2[0]); for(int i = 0; i<n2; i++){ A2[i] = a2[i]; } qsort(data, n, sizeof(int), compare); for(int i = 0; i<n; i++){ cout << data[i] << " "; } }
Output
2 2 1 1 8 8 3 5 6 7 9
- Related Articles
- Sort the array of strings according to alphabetical order defined by another string in C++
- Sort an array according to another array in JavaScript
- Sorting an array according to another array using pair in STL in C++
- Sort an array according to count of set bits in C++
- Sort an array of strings according to string lengths in C++
- C program to sort an array in an ascending order
- C# program to sort an array in descending order
- C program to sort an array in descending order
- Sort an array in descending order using C#
- C++ Program to Sort the Elements of an Array in Descending Order
- C++ Program to Sort the Elements of an Array in Ascending Order
- C program to sort an array by using merge sort
- Java Program to sort an array in alphabetical order
- C program to sort an array of ten elements in an ascending order
- Sort the second array according to the elements of the first array in JavaScript

Advertisements