GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BAEL-5558: Sorting By Date in Java (#12132) · FEDENG01/tutorials@90a6712

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • core-java-modules/core-java-collections-4
    • pom.xml
    • src
      • main/java/com/baeldung/collections/sorting
        • Employee.java
      • test/java/com/baeldung/collections/sorting
        • EmployeeSortingByDateUnitTest.java
  • persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria
    • EmployeeCriteriaIntegrationTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎core-java-modules/core-java-collections-4/pom.xml‎

Copy file name to clipboardExpand all lines: core-java-modules/core-java-collections-4/pom.xml
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@
1414
<version>0.0.1-SNAPSHOT</version>
1515
</parent>
1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>commons-lang</groupId>
20+
<artifactId>commons-lang</artifactId>
21+
<version>2.2</version>
22+
</dependency>
23+
</dependencies>
24+
1725
</project>
Collapse file

‎core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java‎

Copy file name to clipboard
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.collections.sorting;
2+
3+
import java.util.Date;
4+
5+
public class Employee implements Comparable<Employee>{
6+
7+
private String name;
8+
private int age;
9+
private double salary;
10+
private Date joiningDate;
11+
12+
public Employee(String name, int age, double salary, Date joiningDate) {
13+
this.name = name;
14+
this.age = age;
15+
this.salary = salary;
16+
this.joiningDate = joiningDate;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public int getAge() {
28+
return age;
29+
}
30+
31+
public void setAge(int age) {
32+
this.age = age;
33+
}
34+
35+
public double getSalary() {
36+
return salary;
37+
}
38+
39+
public void setSalary(double salary) {
40+
this.salary = salary;
41+
}
42+
43+
public Date getJoiningDate() {
44+
return joiningDate;
45+
}
46+
47+
public void setJoiningDate(Date joiningDate) {
48+
this.joiningDate = joiningDate;
49+
}
50+
51+
@Override
52+
public boolean equals(Object obj) {
53+
return ((Employee) obj).getName()
54+
.equals(getName());
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return new StringBuffer().append("(")
60+
.append(getName()).append(",")
61+
.append(getAge())
62+
.append(",")
63+
.append(getSalary()).append(",").append(getJoiningDate())
64+
.append(")")
65+
.toString();
66+
}
67+
68+
@Override
69+
public int compareTo(Employee employee) {
70+
return getJoiningDate().compareTo(employee.getJoiningDate());
71+
}
72+
}
Collapse file

‎core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java‎

Copy file name to clipboard
+146Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.baeldung.collections.sorting;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.Comparator;
9+
import java.util.Date;
10+
import java.util.List;
11+
import org.apache.commons.lang.time.DateUtils;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
public class EmployeeSortingByDateUnitTest {
16+
17+
private List<Employee> employees = new ArrayList<>();
18+
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
19+
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
20+
21+
@Before
22+
public void initVariables() {
23+
24+
Collections.addAll(employees,
25+
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
26+
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
27+
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
28+
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
29+
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
30+
new Employee("John", 23, 5000, new Date())
31+
);
32+
33+
Collections.addAll(employeesSortedByDateDesc,
34+
new Employee("John", 23, 5000, new Date()),
35+
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
36+
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
37+
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
38+
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
39+
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
40+
);
41+
42+
Collections.addAll(employeesSortedByDateAsc,
43+
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
44+
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
45+
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
46+
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
47+
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
48+
new Employee("John", 23, 5000, new Date())
49+
);
50+
}
51+
52+
@Test
53+
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
54+
Collections.sort(employees);
55+
56+
assertEquals(employees, employeesSortedByDateAsc);
57+
}
58+
59+
@Test
60+
public void givenEmpList_SortEmpList_thenCheckSortedList() {
61+
62+
Collections.sort(employees, new Comparator<Employee>() {
63+
public int compare(Employee o1, Employee o2) {
64+
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
65+
}
66+
});
67+
68+
assertEquals(employees, employeesSortedByDateAsc);
69+
}
70+
71+
@Test
72+
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
73+
74+
Collections.sort(employees, new Comparator<Employee>() {
75+
public int compare(Employee emp1, Employee emp2) {
76+
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
77+
return 0;
78+
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
79+
}
80+
});
81+
82+
assertEquals(employees, employeesSortedByDateAsc);
83+
}
84+
85+
@Test
86+
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
87+
Collections.sort(employees, Collections.reverseOrder());
88+
89+
assertEquals(employees, employeesSortedByDateDesc);
90+
}
91+
92+
@Test
93+
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
94+
95+
Collections.sort(employees, new Comparator<Employee>() {
96+
public int compare(Employee emp1, Employee emp2) {
97+
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
98+
}
99+
});
100+
101+
assertEquals(employees, employeesSortedByDateDesc);
102+
}
103+
104+
@Test
105+
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
106+
107+
Collections.sort(employees, new Comparator<Employee>() {
108+
public int compare(Employee emp1, Employee emp2) {
109+
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
110+
return 0;
111+
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
112+
}
113+
});
114+
115+
assertEquals(employees, employeesSortedByDateDesc);
116+
}
117+
118+
@Test
119+
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
120+
121+
Collections.sort(employees,
122+
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
123+
124+
assertEquals(employees, employeesSortedByDateDesc);
125+
}
126+
127+
@Test
128+
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
129+
130+
Collections.sort(employees, (emp1, emp2) -> {
131+
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
132+
return 0;
133+
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
134+
});
135+
136+
assertEquals(employees, employeesSortedByDateDesc);
137+
}
138+
139+
@Test
140+
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
141+
Collections.sort(employees,
142+
Comparator.comparing(Employee::getJoiningDate));
143+
assertEquals(employees, employeesSortedByDateAsc);
144+
}
145+
146+
}
Collapse file

‎persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java‎

Copy file name to clipboardExpand all lines: persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@ public void testGetAllEmployeesQuery() {
3232
session.close();
3333
assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray());
3434
}
35-
3635
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.