read

Backward sorting array of string using Swift:

    let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
    func backwards(s1: String, s2: String) -> Bool {
        return s1 > s2
    }
    var reversed = sorted(names, backwards)

Too long.

    reversed = sorted(names, { (s1: String, s2: String) -> Bool in
            return s1 > s2
    })

Let's make it one line.

    reversed = sorted(names, { (s1: String, s2: String) -> Bool in return s1 > s2 } )

Don't be satisfied yet.

    reversed = sorted(names, { s1, s2 in return s1 > s2 } )

You didn't think that's the best we can do, right?

    reversed = sorted(names, { s1, s2 in s1 > s2 } )

Shorter!

    reversed = sorted(names, { $0 > $1 } )  

You can do it!

    reversed = sorted(names, >)

Use the force!

    😄=sorted(🐷,>)

There! We have the winner! Oh, if you see two rectangles on the code above, use Safari or Firefox, please :)

Image

NICNOCQUEE

Blog about iOS, Programming, Japan, or Random Stuff.

« Older Newer »