简体   繁体   中英

Sorting TWO or more arrays in JAVA

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 ie 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 ie 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.

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.

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 Comparable s:

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]);
    }
};

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

we create a class which implements comparator interface and compare 1D array elements on the column passed to the constructor. Now, we can pass an instance of this class to the Arrays.sort function , and the 2D array will sorted on the desired column.
Take a look of this.. sorting

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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