I want to sort array of int, strings or any data type. Therefore I decided to use comparable array as an input to my sort function.

But I get an error when I using it.

Comparable arr[];


the compiler says "comparable is undeclared identifier".

I think I must include specific thing so what's this thing?

Thanks,

1) Make sure you spelled it correctly
2) If its on another file, make sure you import it using #include "insertHeaderNameHere.h"
3) Else post your code

That's my code and I'm still get the same error

#include<iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main(){
   Comparable e[];
   return 0;
}

Maybe your thinking this is java or something. In C++ this is how you would sort any data type:

#include <vector>
#include <algorithm>
using namespace std;

int main(){
 std::vector<int> integerArray;
 integerArray.push_back(1);  
 integerArray.push_back(102);
 integerArray.push_back(-23);
 integerArray.push_back(1);

 std::sort( integerArray.begin(), integerArray.end() ); //sort the integer array 
}

you would do similar for any data type, although you might have to specify an explicit comparator

@firstperson
yeah you are right. I'm thinking that c++ like java and I can't do what you say because
I already implemented all sorting types method. and my problem now is how to sort any data type.

So how can I do that in c++?

Edited 5 Years Ago by eman 22: typing error

in c++ generic funtions catch automatically data type, but to get the right rezult you must comform to client contract, that means you have to provide correct input.

This article has been dead for over six months. Start a new discussion instead.