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 8661017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:23:41+00:00 2026-06-12T16:23:41+00:00

Good evening experts and gurus !! I have an array object that record the

  • 0

Good evening experts and gurus !!

I have an array object that record the following..

This is at record.h

Class Record
{
private:
string name;
int data;
float valueData;
public:
bool operator<(const Record&) const;
}

and the constructor are created at record.cpp

record.cpp i added this

bool Record::operator<(const Record& rhs) const
{
return valueData < rhs.valueData;
}

At main.cpp I created record Array of Size 10

#include "record.h"

Record rec[10];

I did

sort(&rec[0], &rec[2]);

but nothing seems changing or sorted.. i got 3 element , rec[0], rec[1], rec[2] and i want sort them ,but they are of another header file record.h & its record.cpp which describe above.

Original Question
Next is i recorded several value to the object

and now rec got 3 index

rec[0]
name = "jack1"
data = 1
valueData = 20

rec[1]
name = "jack2"
data = 2
valueData = 15

rec[2]
name = "jack3"
data = 3
valueData = 25

What i want to achieve is do a sort that can rearrange this array by valueData highest ascending form so.. it will be

rec[2] then rec[0] then rec[1] ..

but i wanna sort by class array object. and re-arrange the value together , how do i achieve this.

the 3 value is private, so i not sure where do i create the sort function, at main.cpp or at record.cpp , next is how do i sort it so it can output in the way below..

-- Highest to lowest --
1) Name: Jack3, Data = 3, Value =25
2) Name: Jack1 , Data =1 , Value = 20
3) Name: Jack2, Data = 2, Value = 15

Thanks for all help and guide!!

  • 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-12T16:23:42+00:00Added an answer on June 12, 2026 at 4:23 pm

    Use std::sort in conjunction with something that compares two Records. There are at least 3 ways to provide this compoarison.

    1: Implement an operator< on Record:

    class Record
    {
    public:
      bool operator<(const Record& rhs) const
      {
        return valueData < rhs.valurData;
      }
    };
    

    …then:

    sort(&rec[0], &rec[10]);
    

    2: Provide a functor:

    struct compare_records : public std::binary_function<bool, Record, Record>
    {
      bool operator()(const Record& lhs, const Record& rhs) const
      { 
        return lhs.valueData < rhs.valueData;
      }
    };
    

    …then:

    sort_if(&rec[0], &rec[10], compare_records());
    

    3: (If you have a C++11 compiler) Use a lambda:

    sort_if(&rec[0], &rec[10], [](const Record& lhs, const Record& rhs) -> bool
    {
      return lhs.vaklueData < rhs.valueData;
    });
    

    EDIT:

    Here is a complete sample that shows how to use the first method:

    #include <cstdlib>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    class Record
    {
    public:
        string name;
        int data;
        float valueData;
    public:
        bool operator<(const Record& rhs) const
        {
            return valueData < rhs.valueData;
        }
    };
    
    int main()
    {
        Record rec[10];
        rec[0].name = "jack1";
        rec[0].data = 1;
        rec[0].valueData = 20;
    
        rec[1].name = "jack2";
        rec[1].data = 2;
        rec[1].valueData = 15;
    
        rec[2].name = "jack3";
        rec[2].data = 3;
        rec[2].valueData = 25;
    
        sort(&rec[0], &rec[3]);
    
        bool bk = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, In my app that I'm currently developing, I have a class that
Good evening I have a JTable that I built with a TableModel how to
Good evening. For a project, I have to create a system. In this system,
Good evening, i need to know how can i have one .h file that
Good evening, This is my 1st time on this site, I have been programming
Good Evening All, I have a textbox that needs to accept 14 digits plus
Good evening. This code works. It sorts an array of cards by both Suit
Good evening :-)! I have this code to use Drag & Drop method for
Good Evening, The problem is that i have both xcode 3.2 and xcode 4
Good evening all! i have an Ajax call that loads 5 'echo's' from a

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.