Skip to content

Commit 783e29e

Browse files
committed
Sandbox. compareTo
1 parent fe70b12 commit 783e29e

File tree

1 file changed

+43
-0
lines changed
  • SandBox/src/com/rakitov/sandbox/comparators

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.rakitov.sandbox.comparators;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* @author Stanislav Rakitov
9+
*/
10+
public class Woman implements Comparable<Woman>{
11+
private final int age;
12+
13+
public Woman(int age) {
14+
this.age = age;
15+
}
16+
17+
@Override
18+
public int compareTo(Woman woman) {
19+
return this.age - woman.age;
20+
}
21+
22+
public static void main(String[] args) {
23+
List<Woman> women = new ArrayList<>();
24+
women.add(new Woman(57));
25+
women.add(new Woman(27));
26+
women.add(new Woman(17));
27+
women.add(new Woman(43));
28+
29+
System.out.println("Before sort");
30+
women.forEach(System.out::println);
31+
System.out.println("\nAfter sort");
32+
Collections.sort(women);
33+
women.forEach(System.out::println);
34+
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "Woman age of " + age;
40+
}
41+
}
42+
43+

0 commit comments

Comments
 (0)