# Sorting for equal_range()

• 01-15-2012, 12:18 PM
heidik
Sorting for equal_range()
Hello all

I am using 3 vectors each of which is of type struct "ABC". I am trying to find duplicates in each vector using equal_range() method. In order for equal_range() to work, the vectors must be sorted. The criteria for sorting each vector is different, therefore, I can't write a single sort method inside the struct "ABC". Is there any way to write different methods for sorting, one for each vector?

Many Thanks

Code:

```// sort algorithm example #include <iostream> #include <algorithm> #include <vector> using namespace std; struct mystruct { string a; string b; string c; string d; string e; }; Sort function-1 (does it not have to have some relevance to struct mystruct?) {     On basis of variables a, b and c } Sort function-2 (does it not have to have some relevance to struct mystruct?) {     On basis of variables a, d and e } Sort function-3 (does it not have to have some relevance to struct mystruct?) {     On basis of variables b, c and d } int main () {     vector< mystruct > myvector1;   vector< mystruct >::iterator it1;   sort (myvector1.begin(), myvector1.end(),Sort function-1);   vector< mystruct > myvector2;   vector< mystruct >::iterator it2;   sort (myvector2.begin(), myvector2.end(),Sort function-2);   vector< mystruct > myvector3;   vector< mystruct >::iterator it3;   sort (myvector3.begin(), myvector3.end(),Sort function-3);   return 0; }```
• 01-19-2012, 05:07 PM
vijayan
Use the version of std::sort() which takes a predicate, with a different predicate for each of the three vectors.
See: http://www.codeproject.com/KB/cpp/Sort.aspx