DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Oct 2010
Posts
119

## 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;
}```  Reply With Quote

2. Registered User
Join Date
Dec 2007
Posts
401
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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 