[Solved] Sorting elements in struct in C++ [duplicate]


I’ll just give you some consecutive tips:

  1. Create std::vector and push you data into:

    vector<properties> students;
    
  2. Write the function which compares two structures and returns the comparison result. Don’t make it a member of your structure. Notice that I’ve renamed it:

    bool less_than(properties const& first, properties const& second) 
    //my fault. Compiler is trying to call std::less<>() function instead of your.
    {  
      return first.points < second.points;    
    }  
    
  3. Call the std::sort function:

    sort(students.begin(), students.end(), less_than); 
    
  4. The data in your students structure will be sorted in the descending order.

This code works: http://ideone.com/iMWcfi

7

solved Sorting elements in struct in C++ [duplicate]