
- 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 the array of strings according to alphabetical order defined by another string in C++
Suppose we have an array of strings, and another string is there for the reference. We have to take the reference string and using the order of the characters in the reference string we will sort the string array. Here we are considering the strings in the array, and the reference string is in lower case letters.
Suppose the string array is like: [“hello”, “programming”, “science”, “computer”, “india”], the reference string is like: “pigvxbskyhqzelutoacfjrndmw”, After sorting the output string will be like [“programming”, “india”, “science”, “hello”, “computer”]
The task is simple. We have to traverse the reference string, then store the character into the map as key, and the index as value. Now to sort the string, we have to compare the strings based on that map, not the ASCII character ordering. Compare the values mapped to those particular characters in the map, if the character c1 appears before c2, then c1 < c2.
Example
#include <iostream> #include <algorithm> #include <unordered_map> #include <vector> using namespace std; unordered_map<char, int> char_map; bool compare(string c1, string c2) { for (int i = 0; i < min(c1.size(), c2.size()); i++) { if (char_map[c1[i]] == char_map[c2[i]]) continue; return char_map[c1[i]] < char_map[c2[i]]; } return c1.size() < c2.size(); } int main() { string str = "pigvxbskyhqzelutoacfjrndmw"; vector<string> v{ "hello", "programming", "science", "computer", "india" }; char_map.clear(); for (int i = 0; i < str.size(); i++) char_map[str[i]] = i; sort(v.begin(), v.end(), compare); // Print the strings after sorting for (auto x : v) cout << x << " "; }
Output
programming india science hello computer
- Related Articles
- Sort an array according to the order defined by another array in C++
- Sort an array of strings according to string lengths in C++
- C program to sort names in alphabetical order with string functions.
- Java Program to sort an array in alphabetical order
- C program to sort names in alphabetical order
- Sort an array according to another array in JavaScript
- Sort an array of strings in ascending order with each string sorted in descending order
- C program to sort names in alphabetical order using structures
- Print array of strings in sorted order without copying one string into another in C++
- Print common characters of two Strings in alphabetical order in C++
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Sort Array of numeric & alphabetical elements (Natural Sort) JavaScript
- Python code to print common characters of two Strings in alphabetical order
- Java code to print common characters of two Strings in alphabetical order
- Python – Extract Strings with Successive Alphabets in Alphabetical Order
