How to sort an array of String triplets

this is probably what you want:

func sortPluginTripletsByManufacturerAndPlugin(
    plugins:consuming [Triplet<String>]) -> [Triplet<String>]
{
    plugins.sort
    {
        ($0.p0, $0.p2) < ($1.p0, $1.p2)
    }
    return plugins
}

i would also make Triplet<T> a struct and not a class, unless you have a different reason for it to be a class.

the predicate of MutableCollection.sort(by:) should return a boolean indicating if two elements are in ascending order, it should not return a sort key.


notes:

  1. you should use consuming and sort instead of sorted, until someone fixes this bug

  2. swift tuples always compare in lexicographical order, so you can just use ($0.p0, $0.p2) < ($1.p0, $1.p2) as the predicate. you cannot return a sort key because tuples are not Comparable.

3 Likes