5

How do I perform an insert-based algorithm on an array of objects based on a Str...

 2 years ago
source link: https://www.codesd.com/item/how-do-i-perform-an-insert-based-algorithm-on-an-array-of-objects-based-on-a-string-parameter.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I perform an insert-based algorithm on an array of objects based on a String parameter?

advertisements

I have two classes: Movie2 and TestMovie2.

Movie2 is as follows:

public class Movie2
{
    private String title;
    private int year;
    private String studio;

    Movie2(String title, int year, String studio)
    {
        this.title = title;
        this.year = year;
        this.studio = studio;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public int getYear()
    {
        return year;
    }

    public void setYear(int year)
    {
        this.year = year;
    }

    public String getStudio()
    {
        return studio;
    }

    public void setStudio(String studio)
    {
        this.studio = studio;
    }
}

TestMovie2 is as follows:

public class TestMovie2
{
    private static Movie2[] myMovies = new Movie2[10];

    private static void addMovies()
    {
        myMovies[0] = new Movie2("The Muppets Take Manhattan", 2001, "Columbia Tristar");
        myMovies[1] = new Movie2("Mulan Special Edition", 2004, "Disney");
        myMovies[2] = new Movie2("Shrek 2", 2004, "Dreamworks");
        myMovies[3] = new Movie2("The Incredibles", 2004, "Pixar");
        myMovies[4] = new Movie2("Nanny McPhee", 2006, "Universal");
        myMovies[5] = new Movie2("The Curse of the Were-Rabbit", 2006, "Aardman");
        myMovies[6] = new Movie2("Ice Age", 2002, "20th Century Fox");
        myMovies[7] = new Movie2("Lilo & Stitch", 2002, "Disney");
        myMovies[8] = new Movie2("Robots", 2005, "20th Century Fox");
        myMovies[9] = new Movie2("Monsters", 2001, "Pixar");
    }

    private static void printMovies()
    {
        for (Movie2 i : myMovies)
        {
            System.out.printf("\n%-30s%10s%30s", i.getTitle(), i.getYear(), i.getStudio());
        }
    }

    private static void sortTitles()
    {

        for (Movie2 i : myMovies)
        {
        }
    }

    public static void main (String args[])
    {
        addMovies();
        printMovies();
        sortTitles();
    }
}

Within sortTitles(), I'm trying to create an algorithm that will sort the objects (the movies) in a based on their titles. Unfortunately I can't use Arrays.sort(), or anything similar. I would greatly appreciate it if someone could give me an example of a String sorter, then explain how it works line-by-line so I can implement one in my project.

Edit: I can't use Arrays.sort() because it is against the instructions of the assignment.


I believe that the purpose of doing this project is to learn the sorting algorithm. I don't thinks it's a good idea to give you the code, which you won't learn much from it. Here is an easy sorting algorithm called insertion sort, example: http://www.java2novice.com/java-sorting-algorithms/insertion-sort/. You can use String.compareTo(anotherString) to sort your titles.

In addition, there are many other nice sorting algorithm you can use to improve performance, such as merge sort and quick sort. I can help you walk through the algorithm, but not giving you the code. Good luck. :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK