简体   繁体   中英

Sorting class array in ascending order

Quite new to programming and in need of some help, I'm looking to sort an class array in ascending order based on age, but I can only get the code to work in descending order. I may be overlooking something small since I've worked on this far too long so I'd appreciate any help!

#include <iostream>
#include <string>
using namespace std;

class Person 
{
    public:
        string name;
        int age;
    
    
        Person(string name = "empty", int age = 0) 
        {
            setName(name);
            setAge(age);
        }
        void setName(string x) {
            name = x;
        }
        
        string getName() {
            return name;
        }
        void setAge(int y) {
            age = y;
        }
        int getAge() {
            return age;
        }
        void displayinfo()
        {
            cout << "Name: " << name; cout << " Age: " << age << endl;
        }
};
void swap(Person &p, Person &q)
{
 Person temp;
 temp.name = p.name;
 temp.age = p.age;
 p.name = q.name;
 p.age = q.age;
 q.name = temp.name;
 q.age = temp.age;
}

int main() 
{
    
    int userValue;
    Person po("Jessica", 24);
    Person po2("Robert", 49);
    Person po3("Maria", 47);
    Person po4("John", 19);
    Person family[4] = {po,po2,po3,po4}; 
    
    int sort(family[4].getAge()); 
    {
        for(int i = 0; i < 3; i++)
            if (family[i].getAge() < family[i+1].getAge()) 
            {
                swap(family[i], family[i+1]);
            }
        
    }
    for(int i = 0; i < 4; i++)
        family[i].displayinfo();

}

int sort(family[4].getAge()); is not a function declaration (and you can't implement a function inside of another function). It is actually a variable declaration. It is declaring a variable int sort that is initialized with the value from family[4].getAge() (which is undefined behavior since index 4 is out of bounds of your family[] array).

So, you are declaring an unused sort variable, and then you enter your for(int i = 0; i < 3; i++) loop, which DOES NOT perform a full sort of the array. For what you are attempting, use the standard std::sort() algorithm instead, eg:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Person 
{
    public:
        string name;
        int age;
    
        Person(string name = "empty", int age = 0) 
        {
            setName(name);
            setAge(age);
        }

        void setName(string x) {
            name = x;
        }
        
        string getName() const {
            return name;
        }

        void setAge(int y) {
            age = y;
        }

        int getAge() const {
            return age;
        }

        void displayinfo() const
        {
            cout << "Name: " << name; cout << " Age: " << age << endl;
        }
};

int main() 
{
    Person po1("Jessica", 24);
    Person po2("Robert", 49);
    Person po3("Maria", 47);
    Person po4("John", 19);

    Person family[4] = {po1, po2, po3, po4};
    
    std::sort(family, family + 4,
        [](const Person &p1, const Person &p2) {
            return p1.getAge() < p2.getAge(); 
        }
    );

    for(int i = 0; i < 4; i++) {
        family[i].displayinfo();
    }

    return 0;
}

Live Demo

Note that your family[] array holds copies of the Person objects that you initialize it with. To avoid the overhead of those copies, you can sort pointers instead, eg:

int main() 
{
    Person po1("Jessica", 24);
    Person po2("Robert", 49);
    Person po3("Maria", 47);
    Person po4("John", 19);

    Person* family[4] = {&po1, &po2, &po3, &po4};
    
    std::sort(family, family + 4,
        [](const Person *p1, const Person *p2) {
            return p1->getAge() < p2->getAge(); 
        }
    );

    for(int i = 0; i < 4; i++) {
        family[i]->displayinfo();
    }

    return 0;
}

Live Demo

Or, you can get rid of the individual p0... objects altogether and initialize the array directly instead, eg:

int main() 
{
    Person family[4]{
        {"Jessica", 24},
        {"Robert", 49},
        {"Maria", 47},
        {"John", 19}
    };
    
    std::sort(family, family + 4,
        [](const Person &p1, const Person &p2) {
            return p1.getAge() < p2.getAge(); 
        }
    );

    for(int i = 0; i < 4; i++) {
        family[i].displayinfo();
    }

    return 0;
}

Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM