Created
September 16, 2012 17:17
-
-
Save jccarbonfive/3733298 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| public class App { | |
| public static class Person { | |
| private String name; | |
| private int weight; | |
| public Person(String name, int weight) { | |
| this.name = name; | |
| this.weight = weight; | |
| } | |
| public int getWeight() { | |
| return this.weight; | |
| } | |
| } | |
| public static class PersonWeightComparator implements Comparator<Person> { | |
| public int compare(Person a, Person b) { | |
| return a.getWeight() - b.getWeight(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| List<Person> people = new ArrayList<Person>(); | |
| people.add(new Person("a", 120)); | |
| people.add(new Person("b", 200)); | |
| people.add(new Person("c", 180)); | |
| Comparator<Person> comparator = new PersonWeightComparator(); | |
| Collections.sort(people, comparator); | |
| for (Person person : people) { // 120, 180, 200 | |
| System.out.println(person.getWeight()); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| public class App { | |
| public static class StringLengthComparator implements Comparator<String> { | |
| public int compare(String a, String b) { | |
| return a.length() - b.length(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| List<String> strings = new ArrayList<String>(); | |
| strings.add("word"); | |
| strings.add("a"); | |
| strings.add("the"); | |
| Comparator<String> comparator = new StringLengthComparator(); | |
| String longest = Collections.max(strings, comparator); | |
| System.out.println(longest); // word | |
| } | |
| } |
This file contains hidden or 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
| import scala.util.Sorting | |
| object App { | |
| case class User(id: Int, name: String) | |
| object User { | |
| implicit object IdOrdering extends Ordering[User] { | |
| def compare(a: User, b: User): Int = { | |
| a.id.compare(b.id) | |
| } | |
| } | |
| object NameOrdering extends Ordering[User] { | |
| def compare(a: User, b: User): Int = { | |
| a.name.compare(b.name) | |
| } | |
| } | |
| } | |
| def main(args: Array[String]) { | |
| val users = Array(User(1, "b"), User(3, "a"), User(2, "c")) | |
| Sorting.quickSort(users) | |
| users.foreach(println) // User(1, "b"), User(2, "c"), User(3, "a") | |
| Sorting.quickSort(users)(User.NameOrdering) | |
| users.foreach(println) // User(3, "a"), User(1, "b"), User(2, "c") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment