|
| 1 | +package com.hellokoding.java.collections; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class ArrayListSortExample { |
| 7 | + public static void main(String[] args) { |
| 8 | + List<Integer> arrayList1 = new ArrayList<>(List.of(3, 1, 2)); |
| 9 | + |
| 10 | + // sort an ArrayList of numbers in ascending order with Collections.sort() |
| 11 | + Collections.sort(arrayList1); |
| 12 | + System.out.println(arrayList1); // [1, 2, 3] |
| 13 | + |
| 14 | + // sort an ArrayList of numbers in descending order with Collections.sort() |
| 15 | + Collections.sort(arrayList1, Comparator.reverseOrder()); |
| 16 | + System.out.println(arrayList1); // [3, 2, 1] |
| 17 | + |
| 18 | + List<String> arrayList2 = new ArrayList<>(List.of("c", "a", "b")); |
| 19 | + |
| 20 | + // sort an ArrayList of Strings in alphabetical order with sort() |
| 21 | + arrayList2.sort(Comparator.naturalOrder()); |
| 22 | + System.out.println(arrayList2); // [a, b, c] |
| 23 | + |
| 24 | + // sort an ArrayList of Strings in reverse alphabetical order with sort() |
| 25 | + arrayList2.sort(Comparator.reverseOrder()); |
| 26 | + System.out.println(arrayList2); // [c, b, a] |
| 27 | + |
| 28 | + LocalDate ld1 = LocalDate.of(2019, 3, 1); |
| 29 | + LocalDate ld2 = LocalDate.of(2019, 1, 1); |
| 30 | + LocalDate ld3 = LocalDate.of(2019, 2, 1); |
| 31 | + List<LocalDate> arrayList3 = new ArrayList<>(List.of(ld1, ld2, ld3)); |
| 32 | + |
| 33 | + // sort an ArrayList of Dates in chronological order |
| 34 | + arrayList3.sort(Comparator.naturalOrder()); |
| 35 | + System.out.println(arrayList3); // [2019-01-01, 2019-02-01, 2019-03-01] |
| 36 | + |
| 37 | + // sort an ArrayList of Dates in reverse chronological order |
| 38 | + arrayList3.sort(Comparator.reverseOrder()); |
| 39 | + System.out.println(arrayList3); // [2019-03-01, 2019-02-01, 2019-01-01] |
| 40 | + } |
| 41 | + |
| 42 | + static class Employee implements Comparable<Employee> { |
| 43 | + private final Integer id; |
| 44 | + private final String name; |
| 45 | + |
| 46 | + public Employee(Integer id, String name) { |
| 47 | + this.id = id; |
| 48 | + this.name = name; |
| 49 | + } |
| 50 | + |
| 51 | + public Integer getId() { |
| 52 | + return id; |
| 53 | + } |
| 54 | + |
| 55 | + public String getName() { |
| 56 | + return name; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean equals(Object o) { |
| 61 | + if (this == o) return true; |
| 62 | + if (o == null || getClass() != o.getClass()) return false; |
| 63 | + Employee employee = (Employee) o; |
| 64 | + return Objects.equals(id, employee.id) && |
| 65 | + Objects.equals(name, employee.name); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int hashCode() { |
| 70 | + return Objects.hash(id, name); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int compareTo(Employee o) { |
| 75 | + return Comparator.comparing(Employee::getName) |
| 76 | + .compare(this, o); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments