1

I have a Movie class set up and I am trying to create a function to sort an array of movies by release date and then by rating. I have 7 movies in a text document getting stored into my array and getter functions at the ready but I am having trouble converting what little I know about sorting an array of ints into an array where I need to sort strings and then doubles.

Here is my code:


string Movie::getReleaseDate() {
    return releaseDate;
}

double Movie::getRating() {
    return rating;
}

void sortByDateRating(Movie movies[], int SIZE) {
   int i;
   int j;
   int temp;      

   for (i = 1; i < SIZE; ++i) {
      j = i;
      
      while (j > 0 && movies[j] < movies[j - 1]) {

         
         temp = movies[j];
         movies[j] = movies[j - 1];
         movies[j - 1] = temp;
         --j;
      }
   }
}

I plan to call this function from a switch case I have in main. Thanks ahead of time.

2
  • Your sort code looks more like a bubble sort than an insert sort. An insert sort starts a the beginning and searches for a good place to insert. All the elements are shifted down by one (to make room for new data), then the data is inserted into the array. Your code isn't doing this. Commented Oct 22, 2021 at 19:54
  • Looks like the Wikipedia description also uses swapping. Maybe my definition is incorrect. Commented Oct 22, 2021 at 19:57

1 Answer 1

2

Unless you really want to implement the sorting yourself, you can use the STL container std::map or the equivalent container from Boost.

Sign up to request clarification or add additional context in comments.

2 Comments

Or just std::sort the array...
I would preferably like to sort it myself but thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.