how to sort the co-ordinates (x, y) with respect to x? (c++ stl)

Suppose the list of pairs <(1, 3),(3, 2), (2, 4)> and i want to result as
<(1, 3), (2, 4), (3, 2)>. How can i implement it using some STL in C++?

Also sorting with respect to second element of the pair?
like: <(3, 2), (1, 3), (2, 4)> !

1 Like

STL Pairs already have the less than (and other relational) operator defined for them. The behavior is such that a < b iff a.first < b.first || a.first == b.first && a.second < b.second

That definition is compatible with your task, therefore simply firing up a sort() or stable_sort() should work.

#include <vector>
#include <pair>
#include <algorithm>

using namespace std;

...

vector<pair<int, int>> a;
sort(a.begin(), a.end());
2 Likes

@beginnerchef:You can refer to the code written by me :

Link:http://ideone.com/wDn9Ws

4 Likes

@beginnerchef

The solution provided by @knb_dtu is accurate for the question you asked.
But still, if you feel any problem in understanding or implementing like that, you can have a look on this piece of code.

Define a struct in c named point (or anything you wish)

How to define ?

struct point{
int x,y;
}

After that, make a array of elements using line like this : struct point a[10000];

-> according to your useā€¦

Now its turn to sort the data for you, here is the code :

void sort(struct point* data, int n) {

int j,i;

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

if(data*.x>data[j].x)

{

struct data temp = data*;

data* = data[j];

data[j] = temp;

}

}

}

}

Just wrote using an O(n^2) sort algo, you can easily understand, and then work on even implementing merge sort/quick sort using this struct which would be simple if you get the concept. :slight_smile:

In case you dont want to use an stl, look at this.

another alternative approach would be using std::set as the following link illustrates

#include

#include

#include

using namespace std;

// compare function to sort pairs with respect to first element.

bool cmpfunc(const pair<int,int> &u,const pair<int,int> &v)

{
if(u.first<v.first)

 return true;

 return false;

}

// compare function to sort pairs with respect ot second element.

/*
bool cmpfunc (const pair<int,int> &u , const pair<int,int> &v)

{
if(u.second<v.second)

return true;

return false; 

}
*/

int main()
{
//initialize vector of pairs

vector<pair<int,int> v;

sort(v.begin(),v.end(),cmpfunc());

return 0;

}

You can solve it using
[sort][1] function in STL C++.

When you will sort a pair of vectors using sort() then it will automatically sort wrt to first element of pair i.e x.

To sort with respect to y , trick is to pass a compare() function as third parameter in function and work accordingly

 
bool compare(pair p,pair q){
    //to solve w.r.t to y
    return p.second < q.second;
}

Here is complete [implementation][2].
[1]: http://www.cplusplus.com/reference/algorithm/sort/
[2]: http://ideone.com/fdV1pg

thank you for your reply.
may you explain how this sort is working and what if I wish to sort with respect to y value of (x, y)!

what if I wish to sort with respect to y value of (x, y)?