[BAEL-5840] Sort a collection of Objects on multiple fields (#12950)

* [BAEL-5840] Sort a collection of Objects on multiple fields

* Fix maven plugin version

* Fix pom again

* Fix again

* Move to core-java-collections-4 module

* Remove unused files

* Test renaming

* Handle NPE

* Use Nonnull annotation

* remove redundant dependency

* remove redundant scope

* Update pom.xml
This commit is contained in:
Songting Xu 2022-11-19 11:07:29 +08:00 committed by GitHub
parent f9e34aa9f1
commit 032b8e2d64
8 changed files with 128 additions and 1 deletions

View File

@ -20,10 +20,16 @@
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>
<commons-lang.version>2.2</commons-lang.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
</properties>
</project>
</project>

View File

@ -2,8 +2,11 @@ package com.baeldung.collections.sorting;
import java.util.Date;
import javax.annotation.Nonnull;
public class Employee implements Comparable<Employee>{
@Nonnull
private String name;
private Date joiningDate;

View File

@ -0,0 +1,15 @@
package com.baeldung.collections.sorting.multiple;
import java.util.Comparator;
public class CheckFieldsOneByOne implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
int nameCompare = o1.getName().compareTo(o2.getName());
if(nameCompare != 0) {
return nameCompare;
}
return Integer.compare(o1.getAge(), o2.getAge());
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.collections.sorting.multiple;
import java.util.Comparator;
public class ComparatorLambda {
public static Comparator<Person> createEmployeeComparator() {
return Comparator.comparing(Person::getName)
.thenComparing(Person::getAge);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.collections.sorting.multiple;
import java.util.Comparator;
import org.apache.commons.lang3.builder.CompareToBuilder;
public class CompareToBuilderExample implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return new CompareToBuilder()
.append(o1.getName(), o2.getName())
.append(o1.getAge(), o2.getAge())
.build();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.collections.sorting.multiple;
import java.util.Comparator;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
public class ComparisonChainExample implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return ComparisonChain.start()
.compare(o1.getName(), o2.getName(), Ordering.natural().nullsFirst())
.compare(o1.getAge(), o2.getAge()).result();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.collections.sorting.multiple;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.collections.sorting.multiple;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
public class ComparatorsUnitTest {
@Test
public void givenPersonsList_whenComparedOnTwoFields_thenReturnCorrectOrder() {
Person person1 = new Person("John", 21);
Person person2 = new Person("Tom", 20);
// Another employee named John
Person person3 = new Person("John", 22);
List<Comparator<Person>> comparators =
Arrays.asList(new CheckFieldsOneByOne(),
new ComparisonChainExample(),
new CompareToBuilderExample(),
ComparatorLambda.createEmployeeComparator());
// All comparators should produce the same result
for(Comparator<Person> comparator : comparators) {
Assertions.assertIterableEquals(
Arrays.asList(person1, person2, person3)
.stream()
.sorted(comparator)
.collect(Collectors.toList()),
Arrays.asList(person1, person3, person2));
}
}
}