From 84ea0625b1c59d25fc8d1bb8445ec7456a36a8e4 Mon Sep 17 00:00:00 2001 From: Emily Millard Date: Fri, 7 Dec 2018 19:25:42 -0800 Subject: [PATCH 1/2] BAEL-2324 sorting arrays --- .../com/baeldung/sort/ArraySortUnitTest.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java diff --git a/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java b/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java new file mode 100644 index 0000000000..489f1848e9 --- /dev/null +++ b/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java @@ -0,0 +1,118 @@ +package com.baeldung.sort; + +import com.baeldung.arraycopy.model.Employee; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class ArraySortUnitTest { + private Employee[] employees; + private int[] numbers; + private String[] strings; + + @Before + public void setup() { + createEmployeesArray(); + createNumbersArray(); + createStringArray(); + } + + private void createEmployeesArray() { + employees = new Employee[5]; + Employee employee; + for (int i = 1; i <= employees.length; i++) { + employee = new Employee(i, "Emp" + i); + employees[i - 1] = employee; + } + Collections.shuffle(Arrays.asList(employees)); + } + + private void createNumbersArray() { + numbers = new int[]{-8, 7, 5, 9, 10, -2, 3}; + } + + private void createStringArray() { + strings = new String[]{"learning", "java", "with", "baeldung"}; + } + + @Test + public void givenIntArray_whenSortingAscending_thenCorrectlySorted() { + Arrays.sort(numbers); + + assertArrayEquals(new int[]{-8, -2, 3, 5, 7, 9, 10}, numbers); + } + + @Test + public void givenIntArray_whenSortingDescending_thenCorrectlySorted() { + numbers = IntStream.of(numbers).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray(); + + assertArrayEquals(new int[]{10, 9, 7, 5, 3, -2, -8}, numbers); + } + + @Test + public void givenStringArray_whenSortingAscending_thenCorrectlySorted() { + Arrays.sort(strings); + + assertArrayEquals(new String[]{"baeldung", "java", "learning", "with"}, strings); + } + + @Test + public void givenStringArray_whenSortingDescending_thenCorrectlySorted() { + Arrays.sort(strings, Comparator.reverseOrder()); + + assertArrayEquals(new String[]{"with", "learning", "java", "baeldung"}, strings); + } + + @Test + public void givenObjectArray_whenSortingAscending_thenCorrectlySorted() { + Arrays.sort(employees, Comparator.comparing(Employee::getName)); + + Employee[] sorted = new Employee[]{ + new Employee(1, "Emp1"), + new Employee(2, "Emp2"), + new Employee(3, "Emp3"), + new Employee(4, "Emp4"), + new Employee(5, "Emp5") + }; + + assertEquals(sorted[0].getId(), employees[0].getId()); + } + + @Test + public void givenObjectArray_whenSortingDescending_thenCorrectlySorted() { + Arrays.sort(employees, Comparator.comparing(Employee::getName).reversed()); + + Employee[] sorted = new Employee[]{ + new Employee(5, "Emp5"), + new Employee(4, "Emp4"), + new Employee(3, "Emp3"), + new Employee(2, "Emp2"), + new Employee(1, "Emp1") + }; + + assertEquals(sorted[0].getId(), employees[0].getId()); + } + + @Test + public void givenObjectArray_whenSortingMultipleAttributesAscending_thenCorrectlySorted() { + Arrays.sort(employees, Comparator.comparing(Employee::getName).thenComparing(Employee::getId)); + + Employee[] sorted = new Employee[]{ + new Employee(1, "Emp1"), + new Employee(2, "Emp2"), + new Employee(3, "Emp3"), + new Employee(4, "Emp4"), + new Employee(5, "Emp5") + }; + + assertEquals(sorted[0].getId(), employees[0].getId()); + } + +} From 64dd23f8f0915d6aaecdcbcb39360e675eb4f8c5 Mon Sep 17 00:00:00 2001 From: Emily Millard Date: Sun, 16 Dec 2018 18:18:13 -0700 Subject: [PATCH 2/2] BAEL-2324 sorting arrays --- .../baeldung/arraycopy/model/Employee.java | 8 ++++ .../com/baeldung/sort/ArraySortUnitTest.java | 44 ++++--------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/core-java-arrays/src/main/java/com/baeldung/arraycopy/model/Employee.java b/core-java-arrays/src/main/java/com/baeldung/arraycopy/model/Employee.java index 757a8f8ec1..a7592574d9 100644 --- a/core-java-arrays/src/main/java/com/baeldung/arraycopy/model/Employee.java +++ b/core-java-arrays/src/main/java/com/baeldung/arraycopy/model/Employee.java @@ -7,6 +7,14 @@ public class Employee implements Serializable { private int id; private String name; + public Employee() { + } + + public Employee(int id, String name) { + this.id = id; + this.name = name; + } + public int getId() { return id; } diff --git a/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java b/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java index 489f1848e9..59035738fe 100644 --- a/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java +++ b/core-java-arrays/src/test/java/com/baeldung/sort/ArraySortUnitTest.java @@ -5,18 +5,20 @@ import org.junit.Before; import org.junit.Test; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.stream.IntStream; import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; public class ArraySortUnitTest { private Employee[] employees; private int[] numbers; private String[] strings; + private Employee john = new Employee(6, "John"); + private Employee mary = new Employee(3, "Mary"); + private Employee david = new Employee(4, "David"); + @Before public void setup() { createEmployeesArray(); @@ -25,13 +27,7 @@ public class ArraySortUnitTest { } private void createEmployeesArray() { - employees = new Employee[5]; - Employee employee; - for (int i = 1; i <= employees.length; i++) { - employee = new Employee(i, "Emp" + i); - employees[i - 1] = employee; - } - Collections.shuffle(Arrays.asList(employees)); + employees = new Employee[]{john, mary, david}; } private void createNumbersArray() { @@ -74,45 +70,21 @@ public class ArraySortUnitTest { public void givenObjectArray_whenSortingAscending_thenCorrectlySorted() { Arrays.sort(employees, Comparator.comparing(Employee::getName)); - Employee[] sorted = new Employee[]{ - new Employee(1, "Emp1"), - new Employee(2, "Emp2"), - new Employee(3, "Emp3"), - new Employee(4, "Emp4"), - new Employee(5, "Emp5") - }; - - assertEquals(sorted[0].getId(), employees[0].getId()); + assertArrayEquals(new Employee[]{david, john, mary}, employees); } @Test public void givenObjectArray_whenSortingDescending_thenCorrectlySorted() { Arrays.sort(employees, Comparator.comparing(Employee::getName).reversed()); - Employee[] sorted = new Employee[]{ - new Employee(5, "Emp5"), - new Employee(4, "Emp4"), - new Employee(3, "Emp3"), - new Employee(2, "Emp2"), - new Employee(1, "Emp1") - }; - - assertEquals(sorted[0].getId(), employees[0].getId()); + assertArrayEquals(new Employee[]{mary, john, david}, employees); } @Test public void givenObjectArray_whenSortingMultipleAttributesAscending_thenCorrectlySorted() { Arrays.sort(employees, Comparator.comparing(Employee::getName).thenComparing(Employee::getId)); - Employee[] sorted = new Employee[]{ - new Employee(1, "Emp1"), - new Employee(2, "Emp2"), - new Employee(3, "Emp3"), - new Employee(4, "Emp4"), - new Employee(5, "Emp5") - }; - - assertEquals(sorted[0].getId(), employees[0].getId()); + assertArrayEquals(new Employee[]{david, john, mary}, employees); } }