5

我需要对评分最高的电影的 ArrayList 进行排序(5 最好 - 1 最差)。我正在使用 Collections.sort() 但列表是一样的。我究竟做错了什么

films.add(new Film().setRating(1));
films.add(new Film().setRating(2.5));
films.add(new Film().setRating(3.5));
films.add(new Film().setRating(4));
films.add(new Film().setRating(5));
films.add(new Film().setRating(1));
films.add(new Film().setRating(2));
films.add(new Film().setRating(3));
films.add(new Film().setRating(4));
Collections.sort(films, new Comparator<Film>() {
    @Override
    public int compare(Film o1, Film o2) {
        final double film1 = o1.getRating();
        final double film2 = o2.getRating();
        return film1 > film2? 1
                : film1 < film2? -1 : 0;
    }
});
4

4 回答 4

5

使用该Double.compare方法效果很好:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    final List<Film> films = new ArrayList<>();
    films.add(new Film().setRating(1));
    films.add(new Film().setRating(2.5));
    films.add(new Film().setRating(3.5));
    films.add(new Film().setRating(4));
    films.add(new Film().setRating(5));
    films.add(new Film().setRating(1));
    films.add(new Film().setRating(2));
    films.add(new Film().setRating(3));
    films.add(new Film().setRating(4));
    System.out.println(films);
    Collections.sort(films, new Comparator<Film>() {
        @Override
        public int compare(Film o1, Film o2) {
            return Double.compare(o1.getRating(), o2.getRating());
        }
    });
    System.out.println(films);
}

输出:

[1.0, 2.5, 3.5, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0]
[1.0, 1.0, 2.0, 2.5, 3.0, 3.5, 4.0, 4.0, 5.0]

Film我用过的:

private static final class Film {

    double rating;

    public double getRating() {
        return rating;
    }

    public Film setRating(double rating) {
        this.rating = rating;
        return this;
    }

    @Override
    public String toString() {
        return Double.toString(rating);
    }
}

我建议使用Double.compare而不是您的版本,甚至o1.getRating - o2.getRating.

要么是那个,要么你的Film实现是错误的——也许你的例子中的 setter 没有设置任何东西?

于 2013-04-06T12:37:57.837 回答
2

请改用以下方法Double

public static int compare(double d1, double d2){}

或像这样:

class Film implements Comparable<Film>{
    double rating;
    @Override
    public int compareTo(Film o) {
        return Double.compare(rating, o.rating);
    }
}
于 2013-04-06T12:35:36.070 回答
1

您的代码在我的计算机上运行良好,如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class SortFilms {
    public static void main(final String ... args) {
        new SortFilms().sort();
    }

    public void sort() {
        final List<Film> films = new ArrayList<Film>();
        films.add(new Film(1));
        films.add(new Film(2.5));
        films.add(new Film(3.5));
        films.add(new Film(4));
        films.add(new Film(5));
        films.add(new Film(1));
        films.add(new Film(2));
        films.add(new Film(3));
        films.add(new Film(4));
        Collections.sort(films, new Comparator<Film>() {
            @Override
            public int compare(Film o1, Film o2) {
                final double film1 = o1.getRating();
                final double film2 = o2.getRating();
                return film1 > film2? 1
                        : film1 < film2? -1 : 0;
            }
        });

        System.out.println(films);
    }

    private class Film {
        private final double rating;

        public Film(double rating) {
            this.rating = rating;
        }

        public String toString() {
            return "" + rating;
        }

        public Double getRating() {
            return rating;
        }
    }
}

产生:

[1.0, 1.0, 2.0, 2.5, 3.0, 3.5, 4.0, 4.0, 5.0]
于 2013-04-06T12:48:47.110 回答
0

有两种方法可以解决这个问题

  1. 在 Collections.sort 方法的第二个参数中实现比较器接口的比较方法。

  2. 确保您的类实现 Comparable 接口,然后实现 compareTo 方法。

这是两种解决方案的代码。

public class ComparableTest implements Comparable < ComparableTest > {

  String name;
  int marks;

  @Override
  public int compareTo(ComparableTest o) {
    if (this.marks > o.marks)
      return 1;
    return -1;
  }

  public ComparableTest(String name, int marks) {
    this.name = name;
    this.marks = marks;
  }

  public String getName() {
    return name;
  }

  public int getMarks() {
    return marks;
  }

}

package program;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class TempList {

  public static void main(String[] args) {

    /*************************************************/
    /****************Assigning Values*****************/
    /*************************************************/
    String[] names = {
      "A",
      "B",
      "C",
      "D",
      "E"
    };
    List < ComparableTest > objects = new LinkedList < > ();

    /*************************************************/
    /****************Printing Values******************/
    /*************************************************/

    for (String name: names) {
      objects.add(new ComparableTest(name, new Random().nextInt(100)));
    }

    System.out.println("Before Sorting");

    for (ComparableTest object: objects) {
      System.out.println(object.name + " " + object.marks);
    }

    /*************************************************/
    /*************Sorting based on marks**************/
    /*************************************************/

    Collections.sort(objects);
    System.out.println("\nSorting based on Marks");

    for (ComparableTest object: objects) {
      System.out.println(object.name + " " + object.marks);
    }

    /*************************************************/
    /*************Sorting based on names**************/
    /*************************************************/

    System.out.println("\nSorting based on Names");

    Collections.sort(objects, new Comparator < ComparableTest > () {

      @Override
      public int compare(ComparableTest o1, ComparableTest o2) {
        if (o1.name.compareTo(o2.name) < 1) {
          return 1;
        }
        return -1;
      }

    });

    for (ComparableTest object: objects) {
      System.out.println(object.name + " " + object.marks);
    }
  }

}

于 2018-12-14T16:15:29.007 回答