Comparator is used to sort the objects by different attributes. You can simply put lambda in the place where requires Comparator. Lambda is preferred when the comparison is simple. Here are the syntax and examples in Java, JavaScript and Python.
In Java, the syntax to compare two object is (a,b) -> a.id – b.id;
In JavaScript, the syntax is (a,b) => a.id – b.id;
In Python, the syntax is key = lambda x: x.id
Sometime you may have complex comparisons involving multiple attributes. For example, a People class has first name, last name and age. You want sort by the name first and age second. In Python, you can list all the attributes for comparison. For Java and JavaScript, lambda is not that convenient. You need to write custom comparator.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.*; public class DefineComparatorAsLambda { static class Country { protected int id; protected String name; //Constructor, Time O(1), Space O(1) public Country(int id, String name) { this.id = id; this.name = name; } //Override, Time O(1), Space O(1) public String toString() { return "(" + id + " " + name + ")"; } } public static void main(String args[]) { //objects List<Country> countries = new ArrayList<>(); countries.add(new Country(3,"Spain")); countries.add(new Country(1,"Kenya")); countries.add(new Country(4,"Saudi arabia")); countries.add(new Country(2,"Mexico")); countries.add(new Country(2,"American")); //Time O(nlogn) Space O(n), n is number of countries countries.sort((a,b)-> a.id - b.id); System.out.println("Sort countries by Id: "); System.out.println(countries); //Time O(nlogn) Space O(n), n is number of countries countries.sort((a,b)-> a.name.compareTo(b.name)); System.out.println("\nSort countries by name: "); System.out.println(countries); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Country { //Constructor, Time O(1), Space O(1) constructor(id, name) { this.id = id; this.name = name; } } var countries = []; countries.push(new Country(3, 'Spain')); countries.push(new Country(1, 'Kenya')); countries.push(new Country(4, 'Sandi arabia')); countries.push(new Country(2, 'Mexcico')); countries.push(new Country(2, 'America')); //order by id, Time O(nlogn) Space O(n), n is number of countries countries.sort((a, b) => a.id - b.id); console.log("sort countries by id: ") console.log(countries); //order by name, Time O(nlogn) Space O(n), n is number of countries countries.sort((a, b) => a.name > b.name? 1 : a.name < b.name? -1 : 0); console.log("sort countries by name alphabetically : ") console.log(countries); |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Country: def __init__(self, id, name): self.id = id self.name = name def __repr__(self): return '{' + str(self.id) + ', ' + self.name + '}' countries = [] countries.append(Country(3, 'Spain')) countries.append(Country(1, 'Kenya')) countries.append(Country(4, 'Sandi arabia')) countries.append(Country(2, 'Mexcico')) countries.append(Country(2, 'America')) #order by id countries.sort(key = lambda x: x.id) print("sort countries by id: ") print(countries) #order by name countries.sort(key = lambda x: x.name) print("\nsort countries by name: ") print(countries) #order by two countries.sort(key = lambda x: (x.id, x.name)) print("\nsort countries by id and name: ") print(countries) |
Output:
Sort countries by Id:
[(1 Kenya), (2 Mexico), (2 American), (3 Spain), (4 Saudi arabia)]
Sort countries by name:
[(2 American), (1 Kenya), (2 Mexico), (4 Saudi arabia), (3 Spain)]
O Notation:
Time complexity:O(nlogn), n is number of countries
Space complexity: O(n)