[java] sort in list

I tried to collect the methods to sort arrays and lists with java. Personal notes.

Array

ascending order


int[] array = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8 };
Arrays.sort(array);

descending order


Integer[] array = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8 };
Arrays.sort(array, Collections.reverseOrder());

List

ascending order


ArrayList<Integer> al = new ArrayList<>();
al.add(2);
al.add(7);
al.add(1);
al.add(8);
al.add(2);
al.add(8);
Collections.sort(al);

descending order


ArrayList<Integer> al = new ArrayList<>();
al.add(2);
al.add(7);
al.add(1);
al.add(8);
al.add(2);
al.add(8);
Collections.sort(al, Collections.reverseOrder());

Application (user-defined class, multiple values)

application


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) {

		List<Item> itemList = new ArrayList<>();
		itemList.add(new Item(4, "Book", 1000));
		itemList.add(new Item(2, "Bag", 3000));
		itemList.add(new Item(3, "Calendar", 2000));
		itemList.add(new Item(1, "pen", 200));

		//id ascending sort
		itemList.sort(Comparator.comparing(Item::getId));

		//id descending sort
		itemList.sort(Comparator.comparing(Item::getId).reversed());

		//kind ascending sort
		itemList.sort(Comparator.comparing(Item::getKind));

		//kind descending sort
		itemList.sort(Comparator.comparing(Item::getKind).reversed());

	}
}

class Item {
	int id;
	String kind;
	int price;

	Item(int id, String kind, int price) {

		this.id = id;
		this.kind = kind;
		this.price = price;
	}

	public int getId() {
		return id;
	}

	public String getKind() {
		return kind;
	}

}

Recommended Posts

[java] sort in list
[Neta] Sleep Sort in Java
List aggregation in Java (Collectors.groupingBy)
Java List Group, Sort, etc.
Sort a List of Java objects
List of members added in Java 9
Sort in List, for personal notes
List of types added in Java 9
java bubble sort
[Java, stream] Sort object list in Japanese by property name
Partization in Java
Java memorandum (list)
Clone Java List.
Changes in Java 11
Rock-paper-scissors in Java
java selection sort
java insertion sort
Pi in Java
FizzBuzz in Java
About List [Java]
Change List <Optional <T >> to Optional <List <T >> in Java
Immutable (immutable) List object conversion function in Java8
Arrylist and linked list difference in java
Process every arbitrary number in Java List
Do not declare variables in List in Java
Implemented basic search / sort algorithm in Java
Sort List in descending order in Java and generate a new List non-destructively
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Java Japanese (Kanji) Sort
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
About Java Array List
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11