大佬教程收集整理的这篇文章主要介绍了ios – 如何在Swift / Xcode中对1个数组进行排序,并通过相同的键更改重新排序多个其他数组,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
bool array_multisort ( array &$array1 [,mixed $array1_sort_order = SORT_ASC [,mixed $array1_sort_flags = SORT_REGULAR [,mixed $... ]]] )
它允许您对1个数组进行排序,并根据原始数据中的键更改对其他多个数组进行重新排序.
在Swift / Xcode 7.2中是否有等效的命令?
我目前有一组数组:
名字
年龄
市
国家
活性
Active是用户在我的应用程序中处于活动状态的一系列时间(以秒为单位).我想命令降序或升序,其他数组改变以保持一致.
var names = [ "Paul","John","David" ] var ages = [ 35,42,27 ] let newOrder = names.enumerate().sort({$0.1<$1.1}).map({$0.0}) names = newOrder.map({names[$0]}) ages = newOrder.map({ages[$0]})
[编辑]以下是对该技术的改进:
这是相同的方法,但只需一步完成排序和分配.
(可以重新分配给原始数组或单独的数组)
(firstNames,ages,cities,countries,actives) = {( $0.map{firstNames[$0]},$0.map{ages[$0]},$0.map{Cities[$0]},$0.map{Countries[$0]},$0.map{actives[$0]} )} (firstNames.enumerated().sorted{$0.1<$1.1}.map{$0.0})
[EDIT2]和一个数组扩展,以便在您进行排序时更容易使用:
extension Array where Element:Comparable { func ordering(by order:(Element,Element)->Bool) -> [Int] { return self.enumerated().sorted{order($0.1,$1.1)}.map{$0.0} } } extension Array { func reorder<T>(_ otherArray:inout [T]) -> [Element] { otherArray = self.map{otherArraY[$0 as! Int]} return self } } firstNames.ordering(by: <) .reorder(&firstNames) .reorder(&ages) .reorder(&cities) .reorder(&countries) .reorder(&actives)
结合前两个:
extension Array { func reordered<T>(_ otherArray:[T]) -> [T] { return self.map{otherArraY[$0 as! Int]} } } (firstNames,actives) = {( $0.reordered(firstNames),$0.reordered(ages),$0.reordered(cities),$0.reordered(countries),$0.reordered(actives) )} (firstNames.ordering(by:<))
以上是大佬教程为你收集整理的ios – 如何在Swift / Xcode中对1个数组进行排序,并通过相同的键更改重新排序多个其他数组全部内容,希望文章能够帮你解决ios – 如何在Swift / Xcode中对1个数组进行排序,并通过相同的键更改重新排序多个其他数组所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。