Interfaces and sorting
Comparable
interface interface Comparable<T> {
┌────┘
▼
int compareTo(T o);
}
-
Antisymmetric: sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
-
Transitive:
x.compareTo(y)
> 0 andy.compareTo(z) > 0
⇒x.compareTo(z) > 0
. -
x.compareTo(y)==0
⇒ thatsgn(x.compareTo(z)) == sgn(y.compareTo(z))
, for all z. -
Recommendation:
(x.compareTo(y)==0) == (x.equals(y))
|
Aaron Bernie Eve Laura Peter Tim |
An array of names in random lexicographical order. |
|
|
|
The sorted array's content is being written to standard output. |
No. 179
Understanding Arrays.sort()
Q: |
Consider a simple
We would like to be able sorting
This code compiles well. Execution however fails: Exception in thread "main" java.lang.ClassCastException: de.hdm_stuttgart.mi.sd1.model.Rectangle cannot be cast to java.base/java.lang.Comparable at java.base/java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320) at java.base/java.util.ComparableTimSort.sort(ComparableTimSort.java:188) at java.base/java.util.Arrays.sort(Arrays.java:1248) at de.hdm_stuttgart.mi.sd1.App.test1(App.java:28) at de.hdm_stuttgart.mi.sd1.App.main(App.java:19) Why does this happen? Read |
A: |
Our
This is an attempt casting an |
No. 180
Sorting Rectangle
instances by
width
Q: |
Correct Understanding |
||
A: |
We implement the interface consisting only of one method
This solves the
|
No. 181
Sorting Rectangle
instances by width and
height
Q: |
Our current sorting implementation may be considered
incomplete. We may have multiple
Two We want rectangles of common width to be sorted by height in
ascending order as well. Modify your
|
||||
A: |
We extend our
|
Unsorted | Case sensitive | Case insensitive | Descending |
---|---|---|---|
UK quick hello sign ATM |
ATM UK hello quick sign |
ATM hello quick sign UK |
sign quick hello UK ATM |
Solution: Provide your own Comparator
!
import java.util.Comparator; public class SortCaseInsensitive implements Comparator<String> { ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┛ @Override ▼ ▼ public int compare(final String a, final String b) { return a.toLowerCase().compareTo(b.toLowerCase()); } }
Comparator
in action Case
insensitive sort
Sort
descending by lambda expression
final String[] names = { "UK", "quick", "hello", "sign", "ATM" }; Arrays.sort(names, (a, b) -> b.compareTo(a)); ❶ for (final String n: names) { System.out.println(n); } |
sign quick hello UK ATM |
This lambda expression is equivalent to the following custom comparator: public class SortDescending implements Comparator<String> { @Override public int compare(final String a, final String b) { return b.compareTo(a); // Equivalent to ❶ } } |
No. 182
Adding flexibility in sorting rectangles
Q: |
We want to change the ordering of rectangles in a flexible manner. Consider the following examples:
Define an additional ordering prescription: Rectangle instances shall be sortable by area in descending order in addition to the already defined ordering by width and height. Instances sharing common area shall be sorted first by width and second by height in descending order as well. TipThe |
||||||||||
A: |
For convenience we add a
The intended sorting requires a corresponding class
implementing the
We may now use both ordering prescriptions next to each other:
Side note: Upcoming lambda expressions in Software development 2 allow for defining sorting methods directly without requiring classes. We provide two simple examples:
|