There are two functions. The first one sorts the list by char , and the second one by int , but they have different names. Tell me how to properly overload the function.

 //sort by char void List::SortByChar() { Listnode *current = head; //вспомогательный указатель. Listnode *next = nullptr; //вспомогательный указатель. if (head == NULL) return; else { while (current->next != NULL) { next = current->next; do { if (strcmp(current->name, next->name) > 0) { std::swap(current->name, next->name); std::swap(current->age, next->age); } next = next->next; } while (next != NULL); current = current->next; } } } //sort by int void List::sortByInt() { Listnode *current = head; //вспомогательный указатель. Listnode *next = nullptr; //вспомогательный указатель. if (head == NULL) return; else { while (current->next != NULL) { next = current->next; do { if (current->age > next->age) { std::swap(current->name, next->name); std::swap(current->age, next->age); } next = next->next; } while (next != NULL); current = current->next; } } } 

    2 answers 2

    You have everything exactly the same except for one line. I would add a comparison function that I passed to a single sorting function as a parameter.

    Type

     static bool Listnode::cmpname(Listnode*a, Listnode*b) { return strcmp(a->name, b->name) > 0; } static bool Listnode::cmpage(Listnode*a, Listnode*b) { return a->age > b->age; } typedef bool (*cmpfunc)(Listnode*, Listnode*); void List::SortBy(cmpfunc) { Listnode *current = head; //вспомогательный указатель. Listnode *next = nullptr; //вспомогательный указатель. if (head == NULL) return; else { while (current->next != NULL) { next = current->next; do { if (cmpfunc(current,name)) { std::swap(current->name, next->name); std::swap(current->age, next->age); } next = next->next; } while (next != NULL); current = current->next; } } } 

    Analogue SortByInt with this -

     list.SortBy(cmpage); 

    and SortByChar -

     list.SortBy(cmpname); 

    Moreover, in List::SortBy() you can pass lambda expressions, say

     list.SortBy([](Listnode*a, Listnode*b){ return a->age > b->age; }); 
    • As I understood to make a single function with a parameter, what should I compare? - Ilya
    • Compares this parameter - this is a comparison function . - Harry
    • And what parameters should I write something to the sortBy function I don’t understand. For a student it is somehow difficult - Ilya
    • You simply pass the function to be called for comparison. I wrote in the answer, and how to write such functions, and how to transfer them to the sorting method. The function compares two Listnode as you need it - by name, age, in any order. You simply say - when it will be necessary to compare two nodes - call for this the function that I specified. - Harry
    • understood, thanks for the help and understanding - Ilya

    I decided to practice writing code in C ++, and that's what I got in the context of your question. I relied on your previous question, where you gave the definition of the list structure and some of its methods.

     #include <iostream> #include <cstring> #include <utility> #include <initializer_list> class List { public: enum SortType { SortByName, SortByAge }; typedef std::pair<const char *, unsigned int> Human; public: List() = default; List(const List &) = delete; List & operator =(const List &) = delete; ~List() { while (head) { Person *tmp = head; head = head->next; delete[] tmp->name; delete tmp; } } void push(const char *name, unsigned int age) { push({ name, age }); } void push(const Human &h ) { char *s = new char[std::strlen(h.first) + 1]; std::strcpy(s, h.first); head = new Person{ s, h.second, head }; } std::ostream & show(std::ostream &os = std::cout) const { for (Person *current = head; current; current = current->next) { os << current->name << ' ' << current->age << std::endl; } return os; } void sort(SortType type = SortByName) { bool ( *order_by[2] )( const Human &, const Human & ) = { []( const Human &h1, const Human &h2) { int res = std::strcmp(h1.first, h2.first); return res < 0 || (res == 0 && h1.second < h2.second); }, [](const Human &h1, const Human &h2) { int res = std::strcmp(h1.first, h2.first); return h1.second < h2.second || ( h1.second == h2.second && res < 0); } }; if (type != SortByAge) type = SortByName; for (Person *current = head; current; current = current->next) { for (Person *next = current->next; next; next = next->next) { if (order_by[type](Human(next->name, next->age), Human(current->name, current->age))) { std::swap(current->name, next->name); std::swap(current->age, next->age); } } } } private: struct Person { const char *name; unsigned int age; Person *next; }; Person *head = nullptr; }; int main() { List lst; for (const auto &h : std::initializer_list<List::Human>( { { "Petr", 21 }, { "Ann", 26 }, { "Bob", 24 }, { "Kate", 24 } } ) ) { lst.push(h); } lst.show() << std::endl; lst.sort(); lst.show() << std::endl; lst.sort(List::SortByAge); lst.show() << std::endl; } 

    The output of the program to the console is as follows:

     Kate 24 Bob 24 Ann 26 Petr 21 Ann 26 Bob 24 Kate 24 Petr 21 Petr 21 Bob 24 Kate 24 Ann 26