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?

Recommended Answers

All 5 Replies

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

So why don't you post what you got...

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;
				}
			}
		}
	}
}

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

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.