Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Swift equivalent for Java's Comparator Interface

Tags:

swift

I have the following Java code below that I am trying to convert into Swift accordingly. Can somebody help me on this issue. Thanks

public class CarsSortById implements Comparator<Cars>{

@Override
public int compare(Cars car1, Cars car2) {
    return (car1.getCarId() < car2.getCarId()) ? -1: 
    (car1.getCarId() > car2.getCarId() ) ? 1:0 ;
   }

}
like image 992
Juventus Avatar asked Dec 13 '22 21:12

Juventus


2 Answers

Swift equivalent of Comparable<T>

Is the Comparable protocol. It requires that your type define an < operator. Since it derives from the Equatable protocol, you need to define the == operator, as well:

class Car {
    let id: Int
    // other fields
}

extension Car: Equatable {
    static func == (lhs: Car, rhs: Car) -> Bool {
        return lhs.id == rhs.id
    }
}

extension Car: Comparable {
    static func < (lhs: Car, rhs: Car) -> Bool {
        return lhs.id < rhs.id
    }
}

Swift equivalent of Comparator<T>

In Java, Comparator is an interface that lets you define sorting functions in addition to the one a type defines for itself via the Comparable Interface. Interfaces like this are necessary because Java didn't support lambdas prior to 1.8. And after Java 1.8, interfaces form the basis of lambdas.

Swift doesn't have an equivalent interface (called a protocol, in Swift). There's no need, since Swift can let you just define closures without the @functionalinterface cruft of Java.

Usually, Java Comparators are only used once. In this case, just call sort with a custom sorting closure:

let carsSortedByID = cars.sorted { $0.id < $1.id }

If you need to resume the sorting closure, you could assign it to a variable. Naturally, it would make sense to store these as static variable in an extension on the type that they sort.:

extension Car {
    static let idSorter: (Car, Car) -> Bool = { $0.id < $1.id }
}

let carsSortedByID = cars.sorted(by: Car.idSorter)
like image 155
Alexander Avatar answered Feb 23 '23 20:02

Alexander


As already mentioned in other answers and in the comment, in swift there is nothing for that. It is just closure.

But here is how you define Comparable

class Car {
    var id = -1
}

extension Car: Comparable {
    static func == (lhs: Car, rhs: Car) -> Bool {
        return lhs.id == rhs.id
    }

    static func < (lhs: Car, rhs: Car) -> Bool {
          return lhs.id < rhs.id
    }
}
like image 35
Orkhan Alikhanov Avatar answered Feb 23 '23 19:02

Orkhan Alikhanov