I have a project that needs to take an int "rating" and a string "title" and sort the list by rating then title. Needs major help.
this is all i have so far.

public class Movie
{

private int rank;
private String title;

public Movie(int rank, String title)
{
this.rank = rank;
this.title = title;
}



}
public void main (String [] args)
{

Movie movie[] = new Movie[5];
movie[0] = new Movie(5, "Top Gun");
movie[1] = new Movie(4, "Boondock Saints");
movie[2] = new Movie(3, "School Of Rock");
movie[3] = new Movie(2, "Old School");
movie[4] = new Movie(1, "Full Metal Jacket");

You need to add get, set methods in your class in order to have access to the private variables:

public int getRank() {
   return rank;
}
public void setRank(int rank) {
   this.rank = rank;
}

The same for the title.


Do you know how to sort an array of integers. If yes then it is the same thing. You will have to use the rank variable though to do the comparison:

for (int i=0;i<movie.length;i++) {
  for (int j=i;j<movie.length;j++) {
     // use the movie[i].getRank(), movie[j].getRank() to compare and sort.
   if (movie[i].getRank() > movie[j].getRank()) {

  } else (movie[i].getRank() == movie[j].getRank()) {
      // If they are equal use the title as well
      if  ( movie[i].getTitle().compareTo(movie[j].getTitle()) > 0 ) {
         // the compareTo method is used to compare Strings
      }
  }

  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.