Yerim Kang
Yerim Kang

Reputation: 311

How to sort arrays in a structure by value

I want to sort the students by their scores, and print the students who's score is the highest to the lowest. How can I sort the arrays in a structure by their values?

#include <iostream>

using namespace std;

struct students {
    string name[20];
    int number;
    double score;
};

int main()
{
    struct students stu[20];
    int i;

    for(i = 0; i<20; i++) {
        cout<<"Please enter the name of the student.: ";
        cin>>stu[i].name;
        cout<<"Please enter the student number.: ";
        cin>>stu[i].number;
        cout<<"Please enter the score.: ";
        cin>>stu[i].score;
    }

}

Upvotes: 0

Views: 66

Answers (2)

Tiger Yu
Tiger Yu

Reputation: 764

There are many ways to accomplish this task using std::sort.

  1. adding a operator < () to struct student
struct student {
    string name;
    int number;
    double score = 0;
    bool operator < (const student& other) const {
        return this->score < other.score;
    }
};
// Then just simply use std::sort
std::sort(std::begin(stu), std::end(stu));
  1. If you can not modify struct student, then you can define a stand along compare function
// define a function that compare students by score:
bool compareScore (const student& a, const student& b) {
    return a.score < b.score;
}

// then use std::sort with the compare function 
std::sort(std::begin(stu), std::end(stu), compareScore);
  1. You can also use a functor to do the work
struct studentFunctor {
    bool operator()(const student& a, const student& b) {
        return a.score < b.score;
    }
};

// then use std::sort with the functor 
std::sort(std::begin(stu), std::end(stu), studentFunctor());
  1. Using C++11 lambda, which is the preferred way to do sorting
std::sort(std::begin(stu), std::end(stu),
          [](const student& a, const student& b){return a.score < b.score;})

Upvotes: 2

Ted Lyngmo
Ted Lyngmo

Reputation: 117168

Assuming the student::number is supposed to be unique, this is how you can sort it in three different ways.

#include <algorithm> // std::sort
#include <iostream>
#include <iterator>  // std::begin, std::end
#include <tuple>     // std::tie

// using namespace std; // bad habit

struct student {      // renamed since it contains info about one student only
    std::string name; // not an array of strings
    int number;
    double score;
};

int main()
{
    using std::cin, std::cout;
    student stu[20]; // struct not needed. student is typedef automatically in C++
    int i;

    for(i = 0; i<20; i++) {
        cout<<"Please enter the name of the student.: ";
        cin>>stu[i].name;
        cout<<"Please enter the student number.: ";
        cin>>stu[i].number;
        cout<<"Please enter the score.: ";
        cin>>stu[i].score;
    }

    // three comparison lambdas
    auto cmp_score = [](const student&a , const student& b) {
        return std::tie(a.score, a.number) < std::tie(b.score, b.number);
    };
    auto cmp_number = [](const student&a , const student& b) {
        return a.number < b.number;
    };
    auto cmp_name = [](const student&a , const student& b) {
        return std::tie(a.name, a.number) < std::tie(b.name, b.number);
    };

    // three ways to sort:
    std::sort(std::begin(stu), std::end(stu), cmp_score);
    std::sort(std::begin(stu), std::end(stu), cmp_number);
    std::sort(std::begin(stu), std::end(stu), cmp_name);
}

Upvotes: 1

Related Questions