简体   繁体   English

Java:如何以降序排列数组?

[英]Java: how to order array in descending order?

So I've created a simple program that asks a user to enter 5 genres and then score them out of 10. I've not added any validation, but I'm not worried about that. 因此,我创建了一个简单的程序,要求用户输入5种类型,然后将其打出10分。我没有添加任何验证,但我对此并不担心。 As you can see, I have two arrays: genres[] and score[] . 如您所见,我有两个数组: genres[]score[] Let's say they enter: 假设他们输入:

        [1] : Genre A | 3
        [2] : Genre B | 6
        [3] : Genre C | 2
        [4] : Genre D | 10
        [5] : Genre E | 8

The results should be listed Genre D, E, B, A, C 结果应列出类型D,E,B,A,C

Here's my overall code: 这是我的整体代码:

import java.util.Scanner;

class OrigClass {
    public static void main (String[] args){
        Scanner ScanObj = new Scanner(System.in);
        int count;
        String[] genres;
        genres = new String[5];
        int[] score;
        score = new int[5];
        for (count = 0; count < 5;count++){
            System.out.println("Enter A Genre: ");
            genres[count] = ScanObj.nextLine();
            System.out.println("How much do you like it? ");
            score[count] = ScanObj.nextInt();
            ScanObj.nextLine();
        }

        for (count = 0; count < 5; count++){
            System.out.println(count+1 + ") " + genres[count] + " " + score[count] + "/10");

        }
        ScanObj.close();
    }
}

Is there a clever way to do it in java using some functions or would I have to manually do it by using temporary varibales, if statements etc. I suppose I could also use the bubble sort algorithm which would be fairly easy to implement. 在Java中是否有使用某些功能的聪明方法,还是我必须通过使用临时变量,if语句等手动进行操作。我想我也可以使用气泡排序算法,该算法很容易实现。 So I want to sort the contents of score[] into descending order. 因此,我想将score[]的内容按降序排列。 Any tips would be appreciated. 任何提示将不胜感激。

Rather than having two parallel arrays, create an array of genre/score objects. 与其创建两个并行数组,不如创建一个类型/得分对象数组。

class OrigClass {
    class ScoredGenre {
        public String genre;
        public int score;
    }
    ScoredGenre[] data = new ScoredGenre[5];
    . . .
}

Then you can define a comparator to compare the objects by score and sort accordingly. 然后,您可以定义一个比较器,以按分数比较对象并进行相应排序。

Arrays.sort(data, new Comparator<ScoredGenre>() {
    public int compare(ScoredGenre a, ScoredGenre b) {
        // Note that a and b are reversed to obtain a descending sort
        return Integer.compare(b.score, a.score);
    }
});

Try the method Arrays.sort(Object[]); 尝试使用Arrays.sort(Object[]);方法Arrays.sort(Object[]);

From the javadoc : javadoc

sort 分类

public static void sort(Object[] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. public static void sort(Object [] a)根据对象的自然顺序将指定对象数组升序排序。 All elements in the array must implement the Comparable interface. 数组中的所有元素必须实现Comparable接口。 Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array). 此外,数组中的所有元素必须相互可比较(也就是说,e1.compareTo(e2)不得对数组中的任何元素e1和e2抛出ClassCastException)。 This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. 这样的排序保证是稳定的:相等的元素不会由于排序而重新排序。

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). 排序算法是一种修改的mergesort(其中,如果低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。 This algorithm offers guaranteed n*log(n) performance. 该算法提供了有保证的n * log(n)性能。

i would recommend few changes in your code as follows 我会建议您对代码进行一些更改,如下所示

step 1. 第1步。

Make a bean named Genre with the following thing 用以下内容制作一个名为Genre的bean

String name int score 字符串名称int得分

now create in your main class create an ArrayList 现在在您的主类中创建一个ArrayList

now add the genres as objects 现在将流派添加为对象

step2. 第2步。

now let your bean implement the comparable interface and use the sort method on the objects made in.the main.class and and overide the method of the comparable interface 现在让您的bean实现可比较的接口,并对in.main.class中的对象使用sort方法,并覆盖可比较的接口的方法

i guess it will be compareTo so in tht method compare the scores and it will sorted... 我想这将是compareTo,因此在此方法中比较分数并进行排序...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM