Skip to content
Related Articles

Related Articles

Improve Article
Program for sorting variables of any data type
  • Difficulty Level : Medium
  • Last Updated : 27 Jan, 2018

Write a program for sorting variables of any datatype without the use of std::sort .

Examples:

Input :  2000, 456, -10, 0
Output : -10   0   456   2000    

Input :  "We do nothing"
         "Hi I have something"
         "Hello Join something!"
         "(Why to do work)"
Output :(Why to do work)   
         Hello Join something!
         Hi I have something
         We do nothing   

The examples above show, we can have any data type elements present as an input and output will be in a sorted form of the input data.
The idea here to solve this problem is to make a template.

Method 1 (Writing our own sort) In below code, we have implemented Bubble Sort to sort the array.






// CPP program to sort array of any data types.
#include <bits/stdc++.h>
using namespace std;
  
// Template formed so that sorting of any 
// type variable is possible
template <class T>
void sortArray(T a[], int n)
{   
    // boolean variable to check that 
    // whether it is sorted or not
    bool b = true;
    while (b) {
        b = false;
        for (size_t i=0; i<n-1; i++) {
  
            // swapping the variable
            // for sorting order
            if (a[i] > a[i + 1]) {
                T temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                b = true;
            }
        }
    }
}
  
// Template formed so that sorting of any 
// type variable is possible
template <class T>
void printArray(T a[], int n)
{
    for (size_t i = 0; i < n; ++i) 
        cout << a[i] << "   ";    
    cout << endl;
}
  
// Driver code
int main()
{
    int n = 4;
    int intArr[n] = { 2000, 456, -10, 0 };
    sortArray(intArr, n);
    printArray(intArr, n);
  
    string strArr[n] = { "We do nothing",
                         "Hi I have something",
                         "Hello Join something!",
                        "(Why to do work)" };
    sortArray(strArr, n);
    printArray(strArr, n);
  
    float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };
    sortArray(floatArr, n);
    printArray(floatArr, n);
  
    return 0;
}
Output:
-10   0   456   2000   
(Why to do work)   Hello Join something!   Hi I have something   We do nothing   
-9.7   11.17   11.4   23.4

Method 2 (Using Library Function)
We can use std::sort in C++ to sort array of any data type.




// CPP program to sort array of any data types.
#include <bits/stdc++.h>
using namespace std;
  
// Template formed so that sorting of any 
// type variable is possible
template <class T>
void printArray(T a[], int n)
{
    for (size_t i = 0; i < n; ++i) 
        cout << a[i] << "   ";    
    cout << endl;
}
  
// Driver code
int main()
{
    int n = 4;
    int intArr[n] = { 2000, 456, -10, 0 };
    sort(intArr, intArr + n);
    printArray(intArr, n);
  
    string strArr[n] = { "We do nothing",
                         "Hi I have something",
                         "Hello Join something!",
                        "(Why to do work)" };
    sort(strArr, strArr + n);
    printArray(strArr, n);
  
    float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };
    sort(floatArr, floatArr+n);
    printArray(floatArr, n);
  
    return 0;
}
Output:
-10   0   456   2000   
(Why to do work)   Hello Join something!   Hi I have something   We do nothing   
-9.7   11.17   11.4   23.4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :