If you mean that the initial array of students should not be sorted, but only an array of pointers to the initial array is sorted, then the program, for example, might look like this
#include <iostream> #include <iterator> #include <algorithm> struct Student { std::string name; std::string surname; int test1; }; struct SortStudents { static bool byName(const Student *first, const Student *second) { return first->name < second->name; } static bool bySurname(const Student *first, const Student *second) { return first->surname < second->surname; } static bool byTest(const Student *first, const Student *second) { return first->test1 < second->test1; } }; std::ostream & operator <<(std::ostream &os, Student &st) { return os << st.name << ' ' << st.surname << ' ' << st.test1; } int main() { Student students[] = { { "A", "YY", 10 }, { "C", "ZZ", 6 }, { "B", "XX", 12 } }; const size_t N = sizeof(students) / sizeof(*students); Student *pStudents[N]; for (size_t i = 0; i < N; i++) pStudents[i] = &students[i]; std::cout << "Sorted by name" << std::endl; std::sort(std::begin(pStudents), std::end(pStudents), SortStudents::byName); for (auto p : pStudents) { std::cout << *p << std::endl; } std::cout << "\nSorted by surname" << std::endl; std::sort(std::begin(pStudents), std::end(pStudents), SortStudents::bySurname); for (auto p : pStudents) { std::cout << *p << std::endl; } std::cout << "\nSorted by tests" << std::endl; std::sort(std::begin(pStudents), std::end(pStudents), SortStudents::byTest); for (auto p : pStudents) { std::cout << *p << std::endl; } return 0; }
Output of the program to the console
Sorted by name A YY 10 B XX 12 C ZZ 6 Sorted by surname B XX 12 A YY 10 C ZZ 6 Sorted by tests C ZZ 6 A YY 10 B XX 12