Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:21:49+00:00 2026-06-13T10:21:49+00:00

I have class and this class contains a number. And I have a vector

  • 0

I have class and this class contains a number. And I have a vector contains object pointer of class. And I want to sort that objects according to their numbers. How can I do this?
Thanks for answers.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Course
{
public:
    Course (int code, string const& name) : name(n), code(c) {}
    int getCourseCode() const { return code; }
    string const& getName() const { return name; }

private:
    string name;
    int code;
};

int main()
{
    vector<Course*> cor;
    vector<Course*>::iterator itcor;

    cor.push_back(new Course(3,"first"));
    cor.push_back(new Course(2,"sekond"));
    cor.push_back(new Course(4,"third"));
    cor.push_back(new Course(1,"fourth"));
    cor.push_back(new Course(5,"fifth"));  
    sort (cor.begin(), cor.end());
    for (itcor=cor.begin(); itcor!=cor.end(); ++itcor) {
        cout << *itcor << ' ';
    }
}

For example when I want the sort the objects they are being sorted according to their adresses.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:21:50+00:00Added an answer on June 13, 2026 at 10:21 am

    You can do this in three ways:

    1) Overload the < operator, and call std::sort algorithm. The code will look like this:

    bool operator<(Course *a, Course *b) const {
      // do comparison
      return A_BOOL_VALUE;
    }
    
    std::sort(array_of_courses.begin(),array_of_courses.end());
    

    The first way is wrong, as you can’t overload the < operator in pointers.

    2) Create a compare function, and then call the second version of std::sort. The code looks like this:

    bool compare(Course *a,Course *b) {
      // do comparison
      return A_BOOL_VALUE;
    }
    
    std::sort(array_of_courses.begin(),array_of_courses.end(),compare);
    

    3) Create a compare class, which has it’s () operator overloaded, and then call the third verion of std::sort. The code:

    struct Compare {
      bool operator()(Course *a, Course *b) {
        // do comparison
        return A_BOOL_VALUE;
      }
    };
    
    std::sort(array_of_courses.begin(),array_of_courses.end(),Compare);
    

    Note: the sort function is found at the algorithm header file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains a static number of objects. This class needs
I have a class that contains a vector of object pointers. I have a
I have a class that inherits from a base class (this contains a lot
I have a class that contains this class method: def self.get_event_record(row, participant) event =
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have a class that contains data from some model. This class has metadata
I have a class that contains a boolean field like this one: public class
say, I have a partial class that contains a custom property like this public
I have a class that contains player numbers... public class Game { public int
I have a class that contains a number of properties of type bool. public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.