C++ Program for sorting variables of any data type


We are given with the values of different datatypes like it can be of type integer, float, string, bool, etc. and the task is to sort the variables of any data type using one common method or function and display the result.

In C++, we can use std::sort to sort any type of array which is available in C++ standard template library(STL). By default sort function sorts the array element in ascending order. Sort() function takes three arguments −

Start element in the array list i.e. from where you want to start your sort

End element in the array list i.e. till where you want your sorting to be done

Change the default setting of sort function to descending order by passing the greater() function to sort in descending order.

Example

Input-: int arr[] = { 2, 1, 5, 4, 6, 3}
Output-: 1, 2, 3, 4, 5, 6

Input-: float arr[] = { 30.0, 21.1, 29.0, 45.0}
Output-: 21.1, 29.0, 30.0, 45.0

Input-: string str =  {"tutorials point is best", "tutorials point", "www.tutorialspoint.com"}
Output-: tutorials point   tutorials point is best   www.tutorialspoint.com

 

Approach used in the below program is as follows −

  • Input the variables of different data types like integer, float, string, etc.
  • Apply the sort() function that will sort the elements of any type of array
  • Print the results

Algorithm

Start
Step 1-> create template class for operating upon different type of data Template <class T>
Step 2-> Create function to display the sorted array of any data type
   void print(T arr[], int size)
   Loop For size_t i = 0 and i < size and ++i
      print arr[i]
   End
Step 3-> In main()
   Declare variable for size of an array int num = 6
   Create an array of type integer int arr[num] = { 10, 90, 1, 2, 3 }
   Call the sort function sort(arr, arr + num)
   Call the print function print(arr, num)
   Create an array of type string string str[num] = { "tutorials point is best",  "tutorials point", "www.tutorialspoint.com" }
   Call the sort function sort(str, str + num)
   Call the print function print(str, num)
   Create an array of type float float float_arr[num] = { 32.0, 12.76, 10.00 }
   Call the sort function sort(float_arr, float_arr+num)
   Call the print function print(float_arr, num)
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
// creating variable of template class
template <class T>
void print(T arr[], int size) {
   for (size_t i = 0; i < size; ++i)  
   cout << arr[i] << "   ";    
   cout << endl;
}
int main() {
   int num = 6;
   int arr[num] = { 10, 90, 1, 2, 3 };
   sort(arr, arr + num);
   print(arr, num);
   string str[num] = { "tutorials point is best", "tutorials point", "www.tutorialspoint.com" };
   sort(str, str + num);
   print(str, num);
   float float_arr[num] = { 32.0, 12.76, 10.00 };
   sort(float_arr, float_arr+num);
   print(float_arr, num);
   return 0;
} 

Output

0   1   2   3 10   90
tutorials point   tutorials point is best   www.tutorialspoint.com
10   12.76   32

Updated on: 20-Dec-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements