Checking if an array is sorted in java (#7385)
* SortedArrayChecker * add recursivity method with custom object * change test names * add new case with comparator * update methods with comparable param * format * fix test * change sign by comparable * change employee by object. Rename method
This commit is contained in:
parent
2c53cdd44e
commit
0d7a1747cc
|
@ -2,7 +2,10 @@ package com.baeldung.array;
|
|||
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class SortedArrayChecker {
|
||||
|
||||
boolean isSorted(int[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
@ -22,7 +25,7 @@ public class SortedArrayChecker {
|
|||
return true;
|
||||
}
|
||||
|
||||
boolean isSorted(String[] array, int length) {
|
||||
boolean isSorted(Comparable[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
|
@ -32,40 +35,31 @@ public class SortedArrayChecker {
|
|||
return isSorted(array, length - 1);
|
||||
}
|
||||
|
||||
boolean isSorted(String[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].compareTo(array[i + 1]) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSortedByName(Employee[] array) {
|
||||
boolean isSorted(Comparable[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].getName().compareTo(array[i + 1].getName()) > 0)
|
||||
if (array[i].compareTo(array[i + 1]) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSortedByAge(Employee[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].getAge() > (array[i + 1].getAge()))
|
||||
return false;
|
||||
boolean isSorted(Object[] array, Comparator comparator) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (comparator.compare(array[i], (array[i + 1])) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSortedByAge(Employee[] array, int length) {
|
||||
boolean isSorted(Object[] array, Comparator comparator, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (array[length - 2].getAge() > array[length - 1].getAge())
|
||||
if (comparator.compare(array[length - 2], array[length - 1]) > 0)
|
||||
return false;
|
||||
|
||||
return isSortedByAge(array, length - 1);
|
||||
return isSorted(array, comparator, length - 1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,32 +4,33 @@ import com.baeldung.arraycopy.model.Employee;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SortedArrayCheckerUnitTest {
|
||||
|
||||
public class SortedArrayCheckerUnitTest {
|
||||
private static final int[] INTEGER_SORTED = {1, 3, 5, 7, 9};
|
||||
private static final int[] INTEGER_NOT_SORTED = {1, 3, 11, 7};
|
||||
|
||||
private static final String[] STRING_SORTED = {"abc", "cde", "fgh"};
|
||||
private static final String[] STRING_NOT_SORTED = {"abc", "fgh", "cde", "ijk"};
|
||||
|
||||
private final Employee[] EMPLOYEES_SORTED_BY_NAME = {
|
||||
private static final Employee[] EMPLOYEES_SORTED_BY_NAME = {
|
||||
new Employee(1, "Carlos", 26),
|
||||
new Employee(2, "Daniel", 31),
|
||||
new Employee(3, "Marta", 27)};
|
||||
|
||||
private final Employee[] EMPLOYEES_NOT_SORTED_BY_NAME = {
|
||||
private static final Employee[] EMPLOYEES_NOT_SORTED_BY_NAME = {
|
||||
new Employee(1, "Daniel", 31),
|
||||
new Employee(2, "Carlos", 26),
|
||||
new Employee(3, "Marta", 27)};
|
||||
|
||||
private final Employee[] EMPLOYEES_SORTED_BY_AGE = {
|
||||
private static final Employee[] EMPLOYEES_SORTED_BY_AGE = {
|
||||
new Employee(1, "Carlos", 26),
|
||||
new Employee(2, "Marta", 27),
|
||||
new Employee(3, "Daniel", 31)};
|
||||
|
||||
private final Employee[] EMPLOYEES_NOT_SORTED_BY_AGE = {
|
||||
private static final Employee[] EMPLOYEES_NOT_SORTED_BY_AGE = {
|
||||
new Employee(1, "Marta", 27),
|
||||
new Employee(2, "Carlos", 26),
|
||||
new Employee(3, "Daniel", 31)};
|
||||
|
@ -61,13 +62,18 @@ class SortedArrayCheckerUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenEmployeeArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSortedByName(EMPLOYEES_SORTED_BY_NAME)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSortedByName(EMPLOYEES_NOT_SORTED_BY_NAME)).isEqualTo(false);
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_SORTED_BY_NAME, Comparator.comparing(Employee::getName))).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_NOT_SORTED_BY_NAME, Comparator.comparing(Employee::getName))).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_SORTED_BY_AGE)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_NOT_SORTED_BY_AGE)).isEqualTo(false);
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge))).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_NOT_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge))).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_SORTED_BY_AGE, EMPLOYEES_SORTED_BY_AGE.length)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSortedByAge(EMPLOYEES_NOT_SORTED_BY_AGE, EMPLOYEES_NOT_SORTED_BY_AGE.length)).isEqualTo(false);
|
||||
assertThat(sortedArrayChecker
|
||||
.isSorted(EMPLOYEES_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge), EMPLOYEES_SORTED_BY_AGE.length))
|
||||
.isEqualTo(true);
|
||||
assertThat(sortedArrayChecker
|
||||
.isSorted(EMPLOYEES_NOT_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge), EMPLOYEES_NOT_SORTED_BY_AGE.length))
|
||||
.isEqualTo(false);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue