根据要排序的数组中提供的值对2D数组的第一个元素进行排序

时间:2016-11-24 15:09:54

标签: java android arrays sorting

我正在学习Java语言,我的任务是根据电影的受欢迎程度对电影进行排序,从数据库中检索信息。

到目前为止,我有以下类型为String [20] [6]的2D数组。 20代表里面的String []数组的数量,代表电影,而6代表每个电影的变量,例如:

[0] - path of image for the movie; 
[1] - overview of the movie;
[2] - release date of the movie;
[3] - title of the movie;
[4] - popularity (expressed in a double format) of the movie;
[5] - average rating (expressed in a double format) of the movie.

我想这样做,以便电影序列[20]使得流行度按照降序排列,而不是改变数据。我已经阅读过有关比较器的信息,只是为了弄清楚并试图找出如何正确应用它们而失败。

背后的原因是我正在使用最流行电影的图像填充gridView,我想实现一个函数来按照受欢迎程度或平均投票来对这些电影进行排序(目前它们正在按序列填充数据库提供)。

提前谢谢你。

3 个答案:

答案 0 :(得分:1)

我希望数据结构类似于List<Movie>,以便可以轻松应用更好的排序算法。在这种情况下,您可能会有类似

的内容
Collections.sort(movies, new PopularityComparator());

使用java的本机(高效)排序算法。 PopularityComparator可能是这样的(假设getPopularity()返回Double

public class PopularityComparator implements Comparator<Movie> {
@Override
public int compare(Movie m1, Movie m2) {
    return Double.compare(m2.getPopularity(), m1.getPopularity());
}
}

但严格来说,这个问题,我认为下面的代码应该适合你。

for (int i = 0; i < movies.length; i++) {
        for (int j = i; i < movies.length; j++) {
            if (Double.compare(Double.parseDouble(movies[i][4]), Double.parseDouble(movies[j][4])) >= 0) {
                //swap movies[i] and movies[j]
                String[] temp = movies[i];
                movies[i] = movies[j];
                movies[j] = temp;
            }
        }
    }

答案 1 :(得分:0)

for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 19; j++) {
            if(movies[i][4]>movies[j][4]){
                // swap movies[i] & movies[j] 
            }
        }
    }

使用此代码。希望帮助您。

答案 2 :(得分:0)

我认为最好在java中使用Objects(当我查看代码时我可以更容易地弄清楚我想做什么),而不是如果你必须将它们转换为多维字符串数组。看看这两个解决方案,首先是你想要的,第二个是我的建议。希望它可以帮到你。

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

/**
 *
 * @author Adrian Stefanovski
 */
public class StackOverflow {

    public static String[][] sorting_array = new String[20][6];
    public static int max_popularity = 10;
    public static int max_rating = 10;
    public static ArrayList<MovieSomething> sorting_array_list = new ArrayList<>();

    public static void main(String[] args) {
        //Solution 1
        populate_array();
        print_not_sorted_array();
        sort_array();
        print_sorted_array();
        //Solution 2
        populate_array(); //Use diferent values
        populate_array_list();
        print_not_sorted_array_list();
        sort_array_list();
        print_sorted_array_list();
        to_multi_d_string_array();
    }

    public static void populate_array() {
        for (int i = 0; i < 20; i++) {
            sorting_array[i][0] = "path " + i;
            sorting_array[i][1] = "overview  " + i;
            sorting_array[i][2] = "release date " + i;
            sorting_array[i][3] = "title " + i;
            sorting_array[i][4] = String.valueOf(Math.random() * max_popularity); //popularity  
            sorting_array[i][5] = String.valueOf(Math.random() * max_rating); //average rating
        }
    }

    public static void print_not_sorted_array() {
        System.out.println("Without sorting array");
        for (int i = 0; i < 20; i++) {
            System.out.println(sorting_array[i][0] + " " + sorting_array[i][1] + " " + sorting_array[i][2] + " " + sorting_array[i][3] + " " + sorting_array[i][4] + " " + sorting_array[i][5]);
        }
    }

    public static void sort_array() {
        for (int i = 0; i < 20; i++) {
            for (int j = i + 1; j < 20; j++) {
                if (Double.valueOf(sorting_array[i][4]) < Double.valueOf(sorting_array[j][4])) {
                    rotate_members(i, j);
                }
            }
        }
    }

    public static void rotate_members(int i, int j) {
        String temp_column_0 = sorting_array[i][0];
        String temp_column_1 = sorting_array[i][1];
        String temp_column_2 = sorting_array[i][2];
        String temp_column_3 = sorting_array[i][3];
        String temp_column_4 = sorting_array[i][4];
        String temp_column_5 = sorting_array[i][5];
        sorting_array[i][0] = sorting_array[j][0];
        sorting_array[i][1] = sorting_array[j][1];
        sorting_array[i][2] = sorting_array[j][2];
        sorting_array[i][3] = sorting_array[j][3];
        sorting_array[i][4] = sorting_array[j][4];
        sorting_array[i][5] = sorting_array[j][5];
        sorting_array[j][0] = temp_column_0;
        sorting_array[j][1] = temp_column_1;
        sorting_array[j][2] = temp_column_2;
        sorting_array[j][3] = temp_column_3;
        sorting_array[j][4] = temp_column_4;
        sorting_array[j][5] = temp_column_5;
    }

    public static void print_sorted_array() {
        System.out.println("With sorting array");
        for (int i = 0; i < 20; i++) {
            System.out.println(sorting_array[i][0] + " " + sorting_array[i][1] + " " + sorting_array[i][2] + " " + sorting_array[i][3] + " " + sorting_array[i][4] + " " + sorting_array[i][5]);
        }
    }

    public static void populate_array_list() {
        for (int i = 0; i < 20; i++) {
            sorting_array_list.add(new MovieSomething(sorting_array[i][0], sorting_array[i][1], sorting_array[i][2], sorting_array[i][3], Double.valueOf(sorting_array[i][4]), Double.valueOf(sorting_array[i][5])));
        }
    }

    public static void print_not_sorted_array_list() {
        System.out.println("Without sorting array list");
        sorting_array_list.stream().forEach((movie) -> {
            System.out.println(movie.toString());
        });
    }

    public static void sort_array_list() {
        Collections.sort(sorting_array_list, new Comparator<MovieSomething>() {
            @Override
            public int compare(MovieSomething fruit2, MovieSomething fruit1) {
                if (fruit1.popularity > fruit2.popularity) {
                    return 1;
                } else {
                    return -1;
                }
            }
        }
        );
    }

    public static void print_sorted_array_list() {
        System.out.println("With sorting array list");
        sorting_array_list.stream().forEach((movie) -> {
            System.out.println(movie.toString());
        });
    }

    public static void to_multi_d_string_array() {
        int i = 0;
        for (MovieSomething movie : sorting_array_list) {
            sorting_array[i][0] = movie.path;
            sorting_array[i][1] = movie.overview;
            sorting_array[i][2] = movie.release_date;
            sorting_array[i][3] = movie.title;
            sorting_array[i][4] = String.valueOf(movie.popularity);
            sorting_array[i][5] = String.valueOf(movie.average_rating);
            i += 1;
        }
    }

    public static class MovieSomething {

        public final String path;
        public final String overview;
        public final String release_date;
        public final String title;
        public final double popularity;
        public final double average_rating;

        public MovieSomething(String path, String overview, String release_date, String title, double popularity, double average_rating) {
            this.path = path;
            this.overview = overview;
            this.release_date = release_date;
            this.title = title;
            this.popularity = popularity;
            this.average_rating = average_rating;
        }

        @Override
        public String toString() {
            return "MovieSomething{" + "path=" + path + ", overview=" + overview + ", release_date=" + release_date + ", title=" + title + ", popularity=" + popularity + ", average_rating=" + average_rating + '}';
        }
    }

}