How to sort 1 array in Swift / Xcode and reorder multiple other arrays by the same keys changes

前端 未结 4 1986
北海茫月
北海茫月 2020-12-10 00:01

Sorry for the complex wording of the question. My main experience is with PHP and it has a command called array_multisort. The syntax is below:

bool array_mu         


        
4条回答
  •  庸人自扰
    2020-12-10 00:24

    Edit this is valid for 2 arrays:

    Adding to @AlainT answer, but using zip:

    var names = [ "Paul", "John", "David" ]
    var ages  = [  35,    42,     27 ]
    
    let sortedTuple = zip(names, ages).sort { $0.0.0 < $0.1.0 }
    

    Something more generic:

    names.enumerate().sort({$0.1<$1.1}).map({ (name: $0.1, age: ages[$0.0]) })
    

提交回复
热议问题