Use sort for multidimensional array (array in array) in Swift?

前端 未结 4 1603
萌比男神i
萌比男神i 2021-01-20 06:18

I want to know that how can we use sort or sorted function for multidimensional array in Swift?

For example theirs an array:



        
4条回答
  •  深忆病人
    2021-01-20 06:52

    I think you should use an array of tuples, then you won't have any problems with type casts:

    let array : [(Int, String)] = [
        (5, "test123"),
        (2, "test443"),
        (3, "test663"),
        (1, "test123")
    ]
    
    let sortedArray = array.sorted { $0.0 < $1.0 }
    

    Swift is all about type safety

    (Change sorted to sort if you're using Swift 2.0)

提交回复
热议问题