A
A
Alexey Khodakov2016-06-26 20:59:56
Arrays
Alexey Khodakov, 2016-06-26 20:59:56

How to sort a 2D array in Swift?

Let's say there is an array
var arr:[(type:Int,name:String)] = []
How to sort it in swift (for example, in ascending order) by one of the fields? For example on the field type:Int?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
ManWithBear, 2016-06-26
@alanius

Well, let's start with the fact that this is a one-dimensional array.

arr.sort { (first, second) -> Bool in
    return first.type < second.type
}

If we recall the features of clojures, then we can write it like this:
arr.sort { $0.type < $1.type }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question