-2

I am trying to sort an array of objects by 1 of the objects' properties (3 strings, 1 int, 1 float). I need to sort them by their integers, highest to lowest, and then by their strings, in alphabetical order. I am having trouble understanding how I can access just 1 part of the objects.

Here is all my code, I have included some of the sample code for sorting that was provided.

#include<iostream>
using namespace std;

#include "video.h"

int main() {

  const int MAX = 100;
  Video *video[MAX];  //up to 100 videos

  for(int l = 0; l < MAX; l++)
    {
      video[l] = NULL;
      // cout << video[l] << endl;  //testing if all are equal to 0
    }

  string title;
  string url;
  string desc;
  string sorting;
  float length;
  int rate;

 // cout << "What sorting method would you like to use?" << endl;
  getline(cin, sorting);
  //cout << "Enter the title, the URL, a comment, the length, and a rating for each video" << endl;

  int t = 0;

  while(getline(cin, title))
    {
      getline(cin, url);
      getline(cin, desc);
      cin >> length;
      cin >> rate;
      cin.ignore();
      video[t] = new Video(title, url, desc, length, rate);
      t++;
    }

for(int s = 0; s < t; s++){
  video[s]->print();
  }

for(int e = 0; e < t; e++)
{
delete video[e];
}

// SORTING 

if(sorting == "length") {
int q = 0;
bool Video::longer(Video *video)
{ return m_length > other->m_length; }}

else if(sorting == "rating") {

}

else if(sorting == "title") {
for(int r = 0; r < MAX; r++) {

}

else{
cerr << sorting << " is not a legal sorting method, giving up" << endl;
return 1; }


//this sorts the videos by length
for(int last = num_videos -1; last > 0; last--) {
for(int cur = 0; cur < last, cur++) {
if(videos[cur]->loner(videos[cur+1])) {
swap(videos[cure], videos[cur+1]); }}}
Jessie
  • 1
  • 2
  • There are many similar existing questions (with answers) - eg [here](http://stackoverflow.com/questions/19104153/c-sorting-objects-based-on-two-data-members). If you try to follow that approach and get stuck, show your code and document the specific problem you're having. – Tony Delroy Feb 15 '16 at 02:56
  • I have looked through many posts regarding this, but in none of them the actual process is explained in a way that makes sense to me and can be applied to my specific program. – Jessie Feb 15 '16 at 03:00
  • It would be great if you can show what you have done, just add your current code and we can help you see where it is wrong – Kai Wu Toh Feb 15 '16 at 03:11
  • To make fellow StackOverflow users have an idea what would make sense to you and can be applied to your specific program, I advise you provide enough information. E.g., show your code and highlight the missing piece. –  Feb 15 '16 at 03:14
  • This is the 3rd question you've asked about this code today. – kfsone Feb 15 '16 at 03:32
  • You're right :) I just need some help. I fell behind in my class due to private reasons and am now trying to catch up but the projects are kicking my butt. I'm trying to do as much as I can, but there are a few little things here and there that I just can't seem to figure out. – Jessie Feb 15 '16 at 03:37

2 Answers2

0

You could take a look at std::sort

http://www.cplusplus.com/reference/algorithm/sort/

You can create a custom comparator to compare the part of the objects you wish to compare.

Kai Wu Toh
  • 236
  • 2
  • 17
0

If you want any ordering (sorting) beyond the default (using operator <), you will have to write your own comparison function.

Let's have a class:

class Example
{
public:
  std::string thing1;
  std::string thing2;
  std::string thing3;
  int         int1;
  float       f1;
};

The std::sort function will be passing references to two objects. The comparison function needs to return true if the first argument comes before the second.

bool comparison(const Example& a, const Example& b)
{
  // Compare by their integers.
  if (a.int1 < b.int1)
  {
     return true;
  }
  if (a.int1 > b.int1)
  {
     return false;
  }
  // We can assume the integers are equal.
  // Now to compare by strings.
  if (a.thing1 != b.thing1)
  {
     return a.thing1 < b.thing1;
  }
  // The first strings are equal at this point.
  // Compare the second versus the second.
  // Which is left as an exercise for the reader.
  return false;
}

If there is a std::vector of these objects, then use the std::sort function:

std::vector<Example> many_examples;
std::sort(many_examples.begin(), many_examples.end(), comparison);
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154