按字母顺序排序字符串

时间:2015-03-15 16:53:33

标签: java sorting

以下代码编译,但sortTitles()方法不按照预期的字母顺序对电影的标题进行排序。你会如何修复compareTo()和sortTitles()方法?

Movie2 Class

public class Movie2 implements Comparable<Movie2> {
    // instance variables 
    private String title;
    private int year;
    private String studio;

    public Movie2(String title, int year, String studio) {
        // initialise instance variables
        this.title = title;
        this.year = year;
        this.studio = studio;
    }

    public String toString() {
        String listing;
        listing = title + ", " + year + ", " + studio;

        return listing;
    }

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

    public String getTitle() {
        return title;
    }

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

    public int getYear() {
        return year;
    }

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

    public String getStudio() {
        return studio;
    }

    public int compareTo(Movie2 obj) {
        if (title < obj.getTitle()) {
            return -1;
        }
        else {
            return 1;
        }
    }
}

TestMovie2类

public class TestMovie2 {
    public static void main(String[] args) {
        Movie2[] myMovies = new Movie2[10];
        Movie2[] sorted = new Movie2[10];

        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 Inc.", 2001, "Pixar");

        System.out.println("  Movies ");
        System.out.println("______________________________");
        System.out.println();
        printMovies(myMovies);
        System.out.println();
        System.out.println();

        Movie2[] dest = new Movie2[myMovies.length];
        sortTitles(myMovies, dest);

        System.out.println(" Sorted by title - ascending ");
        System.out.println("______________________________");
        System.out.println();
        printMovies(myMovies);
        System.out.println();
        System.out.println();

        sortYears(myMovies, sorted);
        System.out.println(" Sorted by year - descending");
        System.out.println("______________________________");
        System.out.println();
        printMovies(sorted);
        System.out.println();
        System.out.println();
    }

    public static void sortTitles(Movie2[] myMovies, Movie2[] dest) {
        for (int i = 0; i < myMovies.length; i++) {
            Movie2 next = myMovies[i];
            int insertIndex = 0;
            int k = i;
            while (k > 0 && insertIndex == 0) {
                if (myMovies[k].getTitle().compareTo(dest[k - 1].getTitle()) < 1) {
                    insertIndex = k;
                }
                else {
                    dest[k] = dest[k - 1];
                }
                k--;
            }
            dest[insertIndex] = next;
        }
    }

    public static void printMovies(Movie2[] sorted) {
        for (int i = 0; i < sorted.length; i++)
            System.out.println(sorted[i]);
    }

    public static void sortYears(Movie2[] myMovies, Movie2[] sorted) {
        for (int i = 0; i < myMovies.length; i++) {
            Movie2 next = myMovies[i];
            int insertindex = 0;
            int k = i;
            while (k > 0 && insertindex == 0) {
                if (next.getYear() < sorted[k - 1].getYear()) {
                    insertindex = k;
                }
                else {
                    sorted[k] = sorted[k - 1];
                }
                k--;
            }
            sorted[insertindex] = next;
        }
    }
}

4 个答案:

答案 0 :(得分:1)

使用已定义的String的compareTo方法,如:

public int compareTo(Movie2 obj) { 
    return this.getTitle().compareTo(obj.getTitle());
}

在sortTitles方法中,使用

Arrays.sort(myMovies);//you dont need seperate dest array

答案 1 :(得分:1)

使用String#compareTo()方法

public int compareTo(Movie2 obj)
{
    if (title != null)
        return title.compareTo(obj.getTitle());
    else
        return obj.getTitle() == null ? 0 : -1;
}

Java不支持运算符重载。因此,您无法将String关系运算符进行比较。你应该在那里得到编译时错误。

答案 2 :(得分:0)

首先在compareTo中修复您的Movie2方法(您可能已经完成了,或者它甚至无法编译):

public int compareTo(Movie2 obj)
{
  return title.compareTo(obj.getTitle());
}

然后sortTitles中的TestMovie2方法:

    public static void sortTitles(Movie2[] myMovies, Movie2[] dest)
    {
        for (int i = 0; i < myMovies.length; i++)
            {
                Movie2 next = myMovies[i];
                int insertIndex = 0;
                int k = i;
                while (k>0 && insertIndex == 0)
                    {
                        if (next.getTitle().compareTo(dest[k-1].getTitle()) > -1)
                            {
                                System.out.println("less than or equal");
                                insertIndex = k;
                            }
                        else 
                            {
                                System.out.println("greater than");
                                dest[k] = dest[k-1]; 
                            }
                        k--;
                    }
                dest[insertIndex] = next;
            } 
    }

你应该得到:

 Sorted by title - ascending 
______________________________

Ice Age, 2002, 20th Century Fox
Lilo & Stitch, 2002, Disney
Monsters Inc., 2001, Pixar
Mulan Special Edition, 2004, Disney
Nanny McPhee, 2006, Universal
Robots, 2005, 20th Century Fox
Shrek 2, 2004, Dreamworks
The Curse of the Were-Rabbit, 2006, Aardman
The Incredibles, 2004, Pixar
The Muppets Take Manhattan, 2001, Columbia Tristar

答案 3 :(得分:0)

为什么要将String对象与关系运算符进行比较?您也可以将方法命名为与现有方法相同的方法。那么在你的情况下发生的是你的代码没有从TestingMovie2类调用方法。它调用默认的java.lang.String方法。更改名称并使用String类的 comapreTo 方法。将方法修改为:

public int movieCompareTo(Movie2 obj)
{ 
  if (title.comapreTo(obj.getTitle()) < 0)
    return -1;
  else 
    return 1; 
}

comapreTo 方法:(来自JavaDoc)

  • 此String对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。
  • 如果此String对象按字典顺序排在参数字符串之前,则结果为负整数。
  • 如果此String对象按字典顺序跟随参数字符串,则结果为正整数。
  • 如果字符串相等,结果为零