I have a project where I want to combine 2 different arrays and then be able to sort them with either the int or string values. This is what I have so far.

int [] myRatingList = { 5, 2, 4, 3, 1 };
String [] myTitleList = {"Top Gun" , "School Of Rock" , "Old School" , "Boondock Saints" , "Full Metal Jacket"};

I need to find a way to sort them alphabetically, then in order by rating.
Any suggestions?

You did not paid attention in class when your lecturer explained how to create and use your own objects.

public class Movie{

    private int rank;
    private String title;

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

//some stter and getter methods
}

Then you would have something like

Movie movie[] = new Movie[5];
movie[0] = new Movie(5, "Top Gun");
//other movies added

at the you want to provide one method for sorting in alphabetical order and other for sorting by rank

I have the titles all added, just having hard time writing the method for this

public class example{
This is what i have, I am just throwing things together to try and make it work...

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");
        
    }
private void printArray(movie[] inventory){
for (int i = 0; i < inventory.length; i++){
System.out.println(inventory[i]);
}
}
public void sortIntArray (int [] movie)
	{


		int rating;

                int tem;

		for (int a = 0; a < movie.length - 1; a++)
		{
			for (rating = 0; rating < movie.length - 1; rating++)
			{
				if (movie [rating] > movie [rating + 1])
				{
					tem = movie [rating];
					movie [rating] = movie [rating + 1];
					movie [rating + 1] = tem;
				}
			}
		}
	}
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

public class example{
This is what i have, I am just throwing things together to try and make it work...


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");

}
private void printArray(movie[] inventory){
for (int i = 0; i < inventory.length; i++){
System.out.println(inventory);
}
}
public void sortIntArray (int [] movie)
{


int rating;

int tem;

for (int a = 0; a < movie.length - 1; a++)
{
for (rating = 0; rating < movie.length - 1; rating++)
{
if (movie [rating] > movie [rating + 1])
{
tem = movie [rating];
movie [rating] = movie [rating + 1];
movie [rating + 1] = tem;
}
}
}
}
}

Stop Spamming the forum by creating 3 threads for one problem.

Now the algo which you are using for sorting has time complexity of n^2, therefore it is not advised. If you still want to use it for your case then first of all if you need to pass the argument as

Movie[] movies

instead of

int [] movie

Secondly tem variable should be a object of movie class and then while comparing movie[rating] u need to use movie[rating].rank to compare. This is going to sort them rank wise.
If you want sort them string wise now compare two ranks and if they are equal compare the strings. For comparing strings you can use compareTo() method. I think you can build up upon from here. Still if you have any problems then we are ready to help

This article has been dead for over six months. Start a new discussion instead.