How to sort an array in c++ by the second column in reverse order?

Suppose I have a 2-D array like this -
32 34
99 19
12 45
23 27
I want to sort it on the basis of the second column in reverse order so that it becomes -
12 45
32 34
23 27
99 19

In python I would just do -

a.sort(key = lambda x: x[1])

How to do this in c++ ?

1 Like

use compare function:

    bool compare( pair <int,int> p1,pair<int,int> p2){
`            return p1.second>p2.second;

in main function code use:

 vector< pair<int,int> > v;

We are using vector of pair because, your program needs only 2 columns.


What are vectors and pair? Can we sort an array with this.

since you want in decreasing order, so i modified the compare func.

vector is a dynamic array,

pair is container used to store 2 elements at a time,

1 Like

And do we have to add a parameter in the sort function as compare function.

sort(v.begin(),v.end(),compare), i forgot to add the compare in sort function.

Can we sort an array with this method.

yes we can, try to implement probably you ll understand, are you clear with the idea of vector?? check this link, i’ve implemented it.

Little bit.

If you are talking about sorting array using sort ; Yes you can sort an array by using sort(a,a+n) where n is the size of an array or the first n elements that you want to sort. assuming ‘a’ is an array

Yes, it can be used is static arrays too, I’ve implemented in the link, check it.

Other than using pair you can also use a struct , it not only makes the code more readable but also you need to type less , such as you need to sort a array of points in a 2D plane , x and y sounds better than first and second,

struct point{
    int x;
    int y;

bool compare(const point &p1,const point &p2){
    return p1.x < p2.x;
    /*Sorts the array based on the x in increasing order , change it to y for comparing the second element and for decreasing order you can change the less than operator to greater than*/

//Now you use sort vectors like these

int main(){

Learning to use structs efficiently can save a lot of headaches.

P.S - Best of luck INOI , I am also practicing for it , :stuck_out_tongue:


Thanks. Do you know some youtube videos which can explain pair and vectors well?

I suggest , if you have enough grip in the syntax it is the best website to learn from , the documentation it has about each and every STL element and that too with nice examples is awesome , for videos i would suggest , it introduces to all STL elements and their functions.

PS - I have httracked for offline viewing ,:stuck_out_tongue:


Why use a vector? Earlier I was accustomed to using vectors all the time but I realised the problem with that while solving a program on the IARCS judge, which got a TLE. I learnt about arrays; but since std::array can’t be used in INOI I guess, since they’re not giving C++11 support. Therefore now I’m learning to use C style arrays efficiently. Most cases though that didn’t make that big a difference; but sometimes it does, and that’s hard to spot it.

This answer is similar mostly to @neilit1992 's answer except that it uses a pair<int,int>[] instead of a vector, and therefore requires a known size.

Use an array of pairs and not vector. Replace the begin iterator with your array variable and the end iterator with the last index you want sorted, the last parameter is your comparison function.

Example: std::sort(a, a+x, cmp); where a is your array x is the last index and cmp is your greater than function.

1 Like

how in this way I do not understand could help explain

it had already being provided in the comment, “pair using static array”

Oops, I hadn’t noticed, sorry. Will have to get rid of my habit of using vectors I guess, could you suggest a few tutorials on BITs, and network flow?