Sorting a list with an int array as an element (Java) (Comparator)

Sorting a list with an int array as an element

Try to sort the array in ascending order so that the first element of the array is from 20,30,10 to 10,20,30 as follows:

[20, 3]     [10, 2] [30, 1] → [20, 3] [10, 2]     [30, 1]

Lists that have arrays as elements can be sorted using Comparator in Java. (Other lambda expressions can be used.)

Comparator

Comparator<int[]> comparator = new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[0], o2[0]);
    }
};

You can use a class that implements the Comparator interface to create something like a criteria for comparing magnitudes. This defines a comparison of the size of the array.

comparator<int[]> comparator = new comparator<int[]>() {};In the part, create an instance of the class that implements the comparator interface and define the method at the same time.Anonymous classIs used.

@overrideMethod beingpublic int compare(int[] o1,int[] o2) {} Defines a compare method that compares the magnitude of o1 and o2 of an int type array.

return Integer.compare(o1[0], o2[0])Depending on the size of the int type array o1 and o2, o1[0]And o2[0]It was defined by the size of.



 From the above, we were able to generate a comparator with the size of the 0th element of the array ** as the criterion for the size of the array **.

 You can sort a list whose elements are arrays by adding the comparator defined earlier to the sort argument.

```java
Collections.sort(list, comparator);

Execution example

Based on the 0th element

List<int[]> list = new ArrayList<int[]>();
int[] a = {20, 3};
int[] b = {30, 1};
int[] c = {10, 2};
list.add(a);
list.add(b);
list.add(c);

//[20, 3]
//[30, 1]
//[10, 2]

Comparator<int[]> comparator = new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[0], o2[0]);
    }
};

Collections.sort(list, comparator);

for (int i=0;i<list.size();i++) {
	System.out.println(Arrays.toString(list.get(i)));
}

//[10, 2]
//[20, 3]
//[30, 1]

Based on the first element

Comparator<int[]> comparator = new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[1], o2[1]);
    }
};

Collections.sort(list, comparator);

//[30, 1]
//[10, 2]
//[20, 3]

Based on the 0th element, in descending order

Comparator<int[]> comparator = new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o2[0], o1[0]);
    }
};

Collections.sort(list, comparator);

//[30, 1]
//[20, 3]
//[10, 2]

It often appears in AtCoder.

Recommended Posts

Sorting a list with an int array as an element (Java) (Comparator)
Cast an array of Strings to a List of Integers in Java
Effective Java Item 25 Select a list from an array First half
[Java] How to search for a value in an array (or list) with the contains method
[Java] How to turn a two-dimensional array with an extended for statement
I want to ForEach an array with a Lambda expression in Java
[Java] Create a collection with only one element
[Java] Get a random value from an array
Sorting using java comparator
About Java Array List
Quickly implement a singleton with an enum in Java
I want to make a list with kotlin and java!
Read WAV data as a byte array in Android Java
[Java] List type / Array type conversion
[Java] Adding an element to the Collection causes a compile error
[Java] Shallow copy and deep copy when converting an array to List
Create an immutable class with JAVA
Java lambda expressions learned with Comparator
Build a Java project with Gradle
Java learning memo (creating an array)
Sort a List of Java objects
[Java] Element existence check with Stream
[Java] Declare and initialize an array
Make a list map with LazyMap
[Java] Conversion from array to List
Run an application made with Java8 with Java6
How to make a Java array
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
Invoke the character string passed as an argument as a method with send
How to make an app with a plugin mechanism [C # and Java]