-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceab87d
commit 386b1f6
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
JavaDsaWithTest/src/main/java/org/practice/dsa/collections/ComparatorExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.practice.dsa.collections; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class ComparatorExample { | ||
public static void main(String[] args) { | ||
List<ComparableDemo> demo = new ArrayList<>(); | ||
demo.add(new ComparableDemo(3, "V")); | ||
demo.add(new ComparableDemo(1, "B")); | ||
demo.add(new ComparableDemo(2, "X")); | ||
|
||
// Collections.sort(demo, new Comparator<ComparableDemo>() { | ||
// @Override | ||
// public int compare(ComparableDemo o1, ComparableDemo o2) { | ||
// return o1.getName().compareTo(o2.getName()); | ||
// } | ||
// }); | ||
// short form | ||
Collections.sort(demo, Comparator.comparing(ComparableDemo::getName)); | ||
|
||
System.out.println("Sort by names:"); | ||
demo.forEach(System.out::println); | ||
} | ||
} |