Doubt in sorting a vector of pairs

Hi, i’m trying to sort a vector of pairs, but i’m unable to understand how to do so
Code => https://ide.codingblocks.com/s/123507
The output is not sorted.
How do i sort it(based on the first element of the pair(high to low))?
(I’m trying to use this concept in my question, and i’m unable to figure this part out)

@ayushg1214 If you want to sort in descending order , you will have to pass an extra argument for the comparator. The comparator function would be something like this:
bool sortinrev(const pair<int,int> &a, const pair<int,int> &b)
{
return (a.first > b.first);
}

sort function call will now have an extra argument:
sort(vect.begin(), vect.end(), sortinrev);

It is still not sorting
See this => https://ide.codingblocks.com/s/123507 I updated the code, yet it has not been sorted.

@ayushg1214 https://ide.codingblocks.com/s/123659
Check it now, that way of initializing the vector of pairs was creating the problem.

Okay I see it is now working. Any reason it was not working in the earlier method?
Is there a better way to take input of pairs of integers (in different part of the code) apart from storing the first one in another temp variable and pushing back the pair together?

@ayushg1214 https://ide.codingblocks.com/s/123693
The way you initialized the vector of pairs earlier, in that case a.end() was not working. Check this code now, you will get the reason why the earlier code was not sorting.

Okay, got it. :grinning:

I got the logic to the problem - form-biggest-number wrong.
Can you tell me the approach to this question?

@ayushg1214 For this problem you can treat the array’s elements as strings and sort them in lexicographically decreasing order. For eg. [54, 546, 548, 60] if you sort these in lexicographically decreasing order result will be [60, 548, 546, 54] , then you can print all the elements and it will form the biggest number.

I can’t figure out how to sort them. Can you please help me?

Just store the numbers in the form of strings in array. Then sort it using stl sort function sort(arr,arr+n,mycompare) using comparator

I’m trying to submit this code, but I get a run-error in this. Even though custom test case pass, it gets the error in testcase #1 only. What’s the problem now?

Nvm Got it, I was using int instead of long long, i changed stoi to stoll and it worked!