0

the class(staff) contains objects int ID, string Name, string Class The vector contains

vector <staff> s = { {234, "Mark", "biology"},
{3455, "Mitch", "English"},
{1234, "Hen", "Maths"}}

How can I sort this from ID? and print sorted? Thank you

2 Answers2

0

I'm sure that this has been answered before, but for your specific case;

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

struct staff {
    int ID;
    std::string Name;
    std::string Class;
};

int main() {
    std::vector <staff> s = { 
        { 234, "Mark", "biology" },
        { 3455, "Mitch", "English" }, 
        { 1234, "Hen", "Maths" }
    };

    std::sort(s.begin(), s.end(), []( const auto& a, const auto& b) { return a.ID < b.ID; });

    for (const auto& staff_member : s) 
        std::cout << staff_member.ID << ": " << staff_member.Name << ", " << staff_member.Class << "\n";

    return 0;
}

This uses the std::sort algorithm from the <algorithm> header, invoked with a comparison function that returns true when a is considered smaller than b.

Bas Groothedde
  • 402
  • 4
  • 8
0

The STL provides std::sort to sort containers (see here for more information). It utilizes the operator< to sort the elements within the container and there you can specify the elements used for sorting.

After calling std::sort the container is sorted and you can print it sorted by iterating over it.

Here is a quick and full example:

#include <iostream>
#include <vector>
#include <algorithm>

class staff {
 public:
  explicit staff(const uint32_t id, const std::string& name, 
      const std::string& class_type) 
    : id_(id), name_(name), class_(class_type) {}
  
  bool operator<(const staff& other) {
      return id_ < other.id_; // sort by id
  }
  
  void print() const {
      std::cout << "ID: " << id_ 
                << ", name: " << name_ 
                << ", class: " << class_ << "\n";
  }
  
 private:
  uint32_t id_;
  std::string name_;
  std::string class_;
};

static void print_staffs(const std::vector<staff>& staffs) {
    for (const staff& staff : staffs) {
        staff.print();
    }
    std::cout << "----------\n";
}

int main()
{    
  std::vector<staff> staffs = { staff(234, "Mark", "biology"),
                                staff(3455, "Mitch", "English"),
                                staff(1234, "Hen", "Maths") };
   
  print_staffs(staffs);                         // print unsorted               
  std::sort(staffs.begin(), staffs.end());      // sort
  print_staffs(staffs);                         // print sorted 
        
  return 0;
}

This yields:

ID: 234, name: Mark, class: biology                                                                                                                                                                                                                                                                                            
ID: 3455, name: Mitch, class: English                                                                                                                                                                                                                                                                                          
ID: 1234, name: Hen, class: Maths                                                                                                                                                                                                                                                                                              
----------                                                                                                                                                                                                                                                                                                                     
ID: 234, name: Mark, class: biology                                                                                                                                                                                                                                                                                            
ID: 1234, name: Hen, class: Maths                                                                                                                                                                                                                                                                                              
ID: 3455, name: Mitch, class: English 
Lukas
  • 1,320
  • 12
  • 20