Sorting arrays next to other arrays which are the same
Jun 16, 2019 at 10:30pm UTC
I just wrote a program where program sorts people by time(minutes).But what and where should I add a code, if minutes are the same and I need to sort seconds as well
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct people{
string name;
int min;
int sec;
};
bool comp(const people &p1, const people &p2) {return (p1.min < p2.min); }
int main()
{
int temp;
people z[6];
for (int i=0; i<6; i++)
{
cin>>z[i].name;
cin>>z[i].min;
cin>>z[i].sec;
}
sort(z, z + 6,comp);
cout<<endl;
for (int i=0; i<6; i++)
{
cout<<z[i].name<<" " <<z[i].min<<" " <<z[i].sec<<endl;
}
return 0;
}
/* input for example:
John 19 15
Liza 9 59
Michael 19 45
Kira 2 37
Thomas 5 41
Justas 19 24
\* */
Jun 16, 2019 at 11:29pm UTC
1 2 3
bool comp(const people &p1, const people &p2) {
return p1.min == p2.min ? p1.sec < p2.sec : p1.min < p2.min;
}
Topic archived. No new replies allowed.