Sort Game -- Can someone give an approach

Sort Game

Sanju needs your help! He gets a list of employees with their salary. The maximum salary is 100.

Sanju is supposed to arrange the list in such a manner that the list is sorted in decreasing order of salary. And if two employees have the same salary, they should be arranged in lexicographical manner such that the list contains only names of those employees having salary greater than or equal to a given number x.

1 Like

Hi Mohit,
In this you have to make a class with two members salary and name. Then create an array of objects of this class. And then sort the array on the basis of salary and if two employee have same salary then on the basis of name(lexicographically). And then print those elements of array having salary greater than the given salary.

Thanks Sanyam jain, thanks for your reply. That’s all I needed.

Hi Mohit
I am marking your doubt as resolved. If you face any issue, feel free to reopen it.

whats wrong with my code

#include<bits/stdc++.h>
using namespace std;

bool comp(pair<string,int> p1,pair<string,int> p2)
{
if(p1.second>p2.second)
{
return p1.second<p2.second;
}
else if(p1.second==p2.second)
{
return p1<p2;
}
}

int main() {
int num,n,d;
cin>>num>>n;
vector<pair<string,int>> vec;
for(int i=0; i<n; i++)
{
string s;
cin>>s>>d;
if(d>num)
vec.push_back(make_pair(s,d));
}
sort(vec.begin(),vec.end(),comp);

for(auto it:vec)
{
	cout<<it.first<<" "<<it.second<<endl;
}
return 0;

}

#include<bits/stdc++.h>
using namespace std;

bool compare (const pair<string,int> &p1,
const pair<string,int> &p2)
{
// cout<<(p2.first)<<" "; cout<<endl;4
if(p2.second==p1.second)
{
return (p1.first)<(p2.first);
}

return (p1.second)>(p2.second);

/* else
{
return p1<p2;
}*/

}
int main()
{
int x;
cin>>x;
int n;
cin>>n;
int i;
// pair<string,int>p;
vector<pair<string,int>>v;
for(i=0;i<n;i++)
{
string A;
cin>>A;
int a;
cin>>a;
if(a>=x)
{
v.push_back(make_pair(A,a));
}

}

sort(v.begin(),v.end(),compare);
for(i=0;i<v.size();i++)
{
cout<<v[i].first<<" "<<v[i].second<<endl;
}
}