-3

I have 10 arrays like this:

    #include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sstream>

using namespace std;

struct person
{
    int age, i;
    char name[20], dob[20], pob[20], gender[7];
};

int main ()
{
    int i = 0;
    person person[10];
    for (i; i<10; i++)
    {
        cout << "Please enter your name, date of birth, place of birth, gender, and age, separated by a space.\nFor example, John 1/15/1994 Maine Male 20: ";
        scanf("%s %s %s %s %d", &person[i].name, &person[i].dob, &person[i].pob, &person[i].gender, &person[i].age);
        printf("%s %s %s %s %d \n", &person[i].name, &person[i].dob, &person[i].pob, &person[i].gender, person[i].age);
    }
    sort(person)
    return 0;
}

How can I sort all of these arrays by age? Age is an int, least to greatest? Thanks.

johnny880
  • 33
  • 6

2 Answers2

0

create a lambda as

auto func = [](Person p1, Person p2)
{
   return p1.age1 < p2.age2;
}

use std::sort to sort the items

std::sort(person.begin(),person.end(),func);

hope it helps

Working code will look like

#include <iostream>
#include <algorithm>
#include <array>

struct Person{
    std::string name_, dob_, pob_;
    char gender_;
    int age_;

    Person(std::string name,std::string dob, std::string pob,char gender,int age)
    :name_(name),dob_(dob),pob_(pob),gender_(gender),age_(age)
    {
    }

    void print() const
    {
        std::cout<<name_<<" , "<<dob_<<" , "<<pob_<<" , "<<gender_<<" , "<<age_<<'\n'; 
    }
};

int main()
{


    std::array<Person,10> arr{
        Person{"AA","12/12/12","BB",'M',24},
        Person{"AA","12/12/12","BB",'M',23},
        Person{"AA","12/12/12","BB",'M',22},
        Person{"AA","12/12/12","BB",'M',21},
        Person{"AA","12/12/12","BB",'M',20},
        Person{"AA","12/12/12","BB",'M',34},
        Person{"AA","12/12/12","BB",'M',33},
        Person{"AA","12/12/12","BB",'M',32},
        Person{"AA","12/12/12","BB",'M',31},
        Person{"AA","12/12/12","BB",'M',30}
    };
    auto func = [] (Person p1,Person p2)
    {
        return p1.age_ < p2.age_;
    };
    std::sort(arr.begin(),arr.end(),func);

    for(auto const & item:arr)
    {
        item.print();
    }
}
g-217
  • 2,069
  • 18
  • 33
  • i don't get it? Sorry, can you implement it into the code? Thanks. – johnny880 Nov 11 '15 at 04:09
  • Edited OP. Maybe i didn't explain clearly. please look at it again – johnny880 Nov 11 '15 at 04:32
  • 1
    You first read about basic input/output in C, see `scanf` and `printf` line in your code. once you'll read the data correctly any of the solution suggested here will work. – g-217 Nov 11 '15 at 04:44
0

Use the std::sort(begin, end, comparator) function that comes with #include<algorithm> and provide your custom comparator which in this case would be

bool comparator(Person i, Person j) {
     return i.age < j.age;
}

std::sort(&Person[0], &Person[len], comparator);
//better alternative
std::sort(std::begin(Person), std::end(Person), comparator);
ffff
  • 2,853
  • 1
  • 25
  • 44
  • i don't get it? Sorry, can you implement it into the code? Thanks. – johnny880 Nov 11 '15 at 04:09
  • 1
    The sort function provides you the functionality of sorting. You just have to pass the start and end of the arrays to it. Since your data is a custom one, it has no way to know how to sort it. You might get abrupt results. That's why the 3rd parameter is a function pointer. The sort function calls this each time it tries comparing two Person objects. This function should return false if the first element is small than second or true otherwise. @johnny880 – ffff Nov 11 '15 at 04:15
  • Edited OP. Maybe i didn't explain clearly. please look at it again – johnny880 Nov 11 '15 at 04:32
  • 1
    @FaizHalde: If `len` is the number of elements, your first example should be `std::sort(&Person[0], &Person[len], comparator);`. The second parameter needs to be a _one-past-the-end_ iterator. – Blastfurnace Nov 11 '15 at 10:06