|
| 1 | +package com.baeldung.sort; |
| 2 | + |
| 3 | +import com.baeldung.arraycopy.model.Employee; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.stream.IntStream; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertArrayEquals; |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +public class ArraySortUnitTest { |
| 16 | + private Employee[] employees; |
| 17 | + private int[] numbers; |
| 18 | + private String[] strings; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void setup() { |
| 22 | + createEmployeesArray(); |
| 23 | + createNumbersArray(); |
| 24 | + createStringArray(); |
| 25 | + } |
| 26 | + |
| 27 | + private void createEmployeesArray() { |
| 28 | + employees = new Employee[5]; |
| 29 | + Employee employee; |
| 30 | + for (int i = 1; i <= employees.length; i++) { |
| 31 | + employee = new Employee(i, "Emp" + i); |
| 32 | + employees[i - 1] = employee; |
| 33 | + } |
| 34 | + Collections.shuffle(Arrays.asList(employees)); |
| 35 | + } |
| 36 | + |
| 37 | + private void createNumbersArray() { |
| 38 | + numbers = new int[]{-8, 7, 5, 9, 10, -2, 3}; |
| 39 | + } |
| 40 | + |
| 41 | + private void createStringArray() { |
| 42 | + strings = new String[]{"learning", "java", "with", "baeldung"}; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenIntArray_whenSortingAscending_thenCorrectlySorted() { |
| 47 | + Arrays.sort(numbers); |
| 48 | + |
| 49 | + assertArrayEquals(new int[]{-8, -2, 3, 5, 7, 9, 10}, numbers); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenIntArray_whenSortingDescending_thenCorrectlySorted() { |
| 54 | + numbers = IntStream.of(numbers).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray(); |
| 55 | + |
| 56 | + assertArrayEquals(new int[]{10, 9, 7, 5, 3, -2, -8}, numbers); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenStringArray_whenSortingAscending_thenCorrectlySorted() { |
| 61 | + Arrays.sort(strings); |
| 62 | + |
| 63 | + assertArrayEquals(new String[]{"baeldung", "java", "learning", "with"}, strings); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenStringArray_whenSortingDescending_thenCorrectlySorted() { |
| 68 | + Arrays.sort(strings, Comparator.reverseOrder()); |
| 69 | + |
| 70 | + assertArrayEquals(new String[]{"with", "learning", "java", "baeldung"}, strings); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void givenObjectArray_whenSortingAscending_thenCorrectlySorted() { |
| 75 | + Arrays.sort(employees, Comparator.comparing(Employee::getName)); |
| 76 | + |
| 77 | + Employee[] sorted = new Employee[]{ |
| 78 | + new Employee(1, "Emp1"), |
| 79 | + new Employee(2, "Emp2"), |
| 80 | + new Employee(3, "Emp3"), |
| 81 | + new Employee(4, "Emp4"), |
| 82 | + new Employee(5, "Emp5") |
| 83 | + }; |
| 84 | + |
| 85 | + assertEquals(sorted[0].getId(), employees[0].getId()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void givenObjectArray_whenSortingDescending_thenCorrectlySorted() { |
| 90 | + Arrays.sort(employees, Comparator.comparing(Employee::getName).reversed()); |
| 91 | + |
| 92 | + Employee[] sorted = new Employee[]{ |
| 93 | + new Employee(5, "Emp5"), |
| 94 | + new Employee(4, "Emp4"), |
| 95 | + new Employee(3, "Emp3"), |
| 96 | + new Employee(2, "Emp2"), |
| 97 | + new Employee(1, "Emp1") |
| 98 | + }; |
| 99 | + |
| 100 | + assertEquals(sorted[0].getId(), employees[0].getId()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void givenObjectArray_whenSortingMultipleAttributesAscending_thenCorrectlySorted() { |
| 105 | + Arrays.sort(employees, Comparator.comparing(Employee::getName).thenComparing(Employee::getId)); |
| 106 | + |
| 107 | + Employee[] sorted = new Employee[]{ |
| 108 | + new Employee(1, "Emp1"), |
| 109 | + new Employee(2, "Emp2"), |
| 110 | + new Employee(3, "Emp3"), |
| 111 | + new Employee(4, "Emp4"), |
| 112 | + new Employee(5, "Emp5") |
| 113 | + }; |
| 114 | + |
| 115 | + assertEquals(sorted[0].getId(), employees[0].getId()); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments