在JAVA中对两个或多个数组进行排序

发布于 2024-12-16 21:08:00 字数 674 浏览 0 评论 0原文

我想根据两个数组的数值对它们进行排序。例如,

apple[]  = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];

我想根据数组中的第三个值对其进行排序,即 16.19

我将如何处理这个问题?

注意数组声明仅用于说明目的。

结果必须如下所示:

String[] apples =     [apples, green, 9180, 16.19];
String[] oranges =    [oranges, orange color, 9180, 25.18];
String[] grapes =     [grapes, green, 9180, 35.16]
String[] strawberry = [strawberries, red, 9180, 45.36]

每一行都是一个单个单独的数组。必须按照最后一个数字升序排序即 16.19, 25.18, 35.16

数组有四个字段 [apples, green, 9180, 16.19];苹果:水果的名称;绿色:水果的颜色; 9180:农场密码; 16.19:果实产量。我必须根据水果的产量对它们进行排序。希望我说清楚了。

I would like to sort two arrays according to their numeric values.For eg

apple[]  = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];

I would like to sort it according to the 3rd value in the array i.e 16.19

How will I go about this ?

Note array declaration is for illustrative purposes only.

The result must look like this:

String[] apples =     [apples, green, 9180, 16.19];
String[] oranges =    [oranges, orange color, 9180, 25.18];
String[] grapes =     [grapes, green, 9180, 35.16]
String[] strawberry = [strawberries, red, 9180, 45.36]

Each line is a single separate array. It must be sorted in ascending order according to the last number i.e 16.19, 25.18, 35.16

The array has four fields [apples, green, 9180, 16.19]; Apples: name of the fruit; Green: color of the fruit; 9180: pin code of farm; 16.19: yield of fruits. I must sort these according to the yield of fruits. Hope I am clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒儿 2024-12-23 21:08:00

我允许自己假设您没有四个名为 orangesapplesstrawberrygrapes 的变量。如果这样做,请在排序之前将它们放入数组中。

Comparable 数组进行排序的示例代码:

class a{

    public static void main(String[]args){

        final int column = 3;
        Comparable[][] a = new Comparable[][]{
            {"oranges", "orange color", 9180, 25.18},
            {"apples", "green", 9180, 16.19},
            {"strawberries", "red", 9180, 45.36},
            {"grapes", "green", 9180, 35.16}
        };

        java.util.Arrays.sort(a, 
            new java.util.Comparator<Comparable[]>(){
                public int compare(Comparable[]a,Comparable[]b){
                    return a[column].compareTo(b[column]);
                }
        });

        for (Comparable[] c : a)
            System.out.println(java.util.Arrays.toString(c));

    }

}

输出:

[apples, green, 9180, 16.19]
[oranges, orange color, 9180, 25.18]
[grapes, green, 9180, 35.16]
[strawberries, red, 9180, 45.36]

如果您想要更通用的东西,您可以定义一个数组比较器,它将比较列作为其构造函数中的参数(请参阅更新示例):

class ArrayComparator implements java.util.Comparator<Comparable[]>{
    private int col;
    public ArrayComparator(int col){
        this.col = col;
    }
    public int compare(Comparable[] o1, Comparable[] o2){
        return o1[col].compareTo(o2[col]);
    }
};

I allowed myself the assumption that you don't have four variables named oranges, apples, strawberries and grapes. If you do, put them inside an array before sorting.

Example code to sort an array of arrays of Comparables:

class a{

    public static void main(String[]args){

        final int column = 3;
        Comparable[][] a = new Comparable[][]{
            {"oranges", "orange color", 9180, 25.18},
            {"apples", "green", 9180, 16.19},
            {"strawberries", "red", 9180, 45.36},
            {"grapes", "green", 9180, 35.16}
        };

        java.util.Arrays.sort(a, 
            new java.util.Comparator<Comparable[]>(){
                public int compare(Comparable[]a,Comparable[]b){
                    return a[column].compareTo(b[column]);
                }
        });

        for (Comparable[] c : a)
            System.out.println(java.util.Arrays.toString(c));

    }

}

Output:

[apples, green, 9180, 16.19]
[oranges, orange color, 9180, 25.18]
[grapes, green, 9180, 35.16]
[strawberries, red, 9180, 45.36]

If you want something even more generic, you can define an array comparator which takes the comparison column as a parameter in its constructor (see updated example):

class ArrayComparator implements java.util.Comparator<Comparable[]>{
    private int col;
    public ArrayComparator(int col){
        this.col = col;
    }
    public int compare(Comparable[] o1, Comparable[] o2){
        return o1[col].compareTo(o2[col]);
    }
};
人疚 2024-12-23 21:08:00

您可以将数组封装在一个类中,该类将实现 可比较的界面。这将使您能够使用您想要的任何比较策略。

You can encapsulate your arrays in a class that will implement the Comparable interface. This will give you the ability to use whatever comparison strategy you want.

像极了他 2024-12-23 21:08:00

解释后更新:
我假设您将这些数组存储在列表或其他数组中。在这种情况下,您可以使用Collections.sort()Arrays.sort()。由于 sort() 只能对元素可相互比较的数组进行排序,因此您必须实现一个 Comparator 来比较数组的最后一个元素和将其传递给排序方法。 比较器示例

Update after explanation:
I assume you store these arrays in a list or an other array. It this case, you can use Collections.sort() or Arrays.sort(). Since sort() can sort only an array whose elements are mutually comparable, you have to implement a Comparator<Object[]> that compares the last elements of the arrays and pass it to the sort method. Example on comparator

南风起 2024-12-23 21:08:00

问题是:

为什么您甚至将带有属性的 Items 封装为数组?我更喜欢一个名为“Fruits”的封装类。在此类中,您可以实现 Comparable Interface 或编写单独的 Comparator。

之后,你会得到这样的东西:

LinkedList<Fruits> myFruits;
Collections.sort(myFruits);

// Optional with passed Comparator, myComparator has to implement Comparator
Collections.sort(myFruits, myComparator)

Question is:

Why do you even encapsulate your Items with attributes as an array? I would prefer a class for encapsulating, called "Fruits". In this class you can implement the Comparable Interface or write a separate Comparator.

After that, you you'll have something like this:

LinkedList<Fruits> myFruits;
Collections.sort(myFruits);

// Optional with passed Comparator, myComparator has to implement Comparator
Collections.sort(myFruits, myComparator)
秋心╮凉 2024-12-23 21:08:00

尝试这个函数

apple[]  = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];

Arrays.sort(apple);
Arrays.sort(orange);

请参阅此处 - 如何对数组进行排序

try this function

apple[]  = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];

Arrays.sort(apple);
Arrays.sort(orange);

See here - how to sort an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文