PKane
PKane

Reputation: 19

How to use insertion sorting on an array of objects?

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.

Upvotes: 1

Views: 322

Answers (1)

Nilsie
Nilsie

Reputation: 100

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

Upvotes: 2

Related Questions