如何排序斯威夫特/ X code 1阵列,并使用相同的按键重新排列变化等多重阵列 [英] How to sort 1 array in Swift / Xcode and reorder multiple other arrays by the same keys changes

查看:257
本文介绍了如何排序斯威夫特/ X code 1阵列,并使用相同的按键重新排列变化等多重阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这个问题的复杂的措辞。我的主要经验是用PHP,它有一个名为在array_multisort命令。语法如下:

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_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] )

它可以让你排序1阵列和基于原有的关键变化重新排序其他多个阵列。

It lets you sort 1 array and the reorder multiple other arrays based on the key changes in the original.

是否有等效的命令雨燕/ X code 7.2?

Is there an equivalent command in Swift / Xcode 7.2?

我现在有一个阵列集:

名字
年龄

国家
主动

FirstName Age City Country Active

活动是一次用户已我的应用程序内的活跃秒的数组。我想下令升序或降序和其他阵列更改为保持一致。

Active is an array of time in seconds that a user has been active within my app. I would like to order that descending or ascending and the other arrays to change to remain consistent.

推荐答案

您可以创建有序索引的数组,并把它作为一个映射:

You could create an array of indexes in sorted order and use it as a mapping:

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]})

这篇关于如何排序斯威夫特/ X code 1阵列,并使用相同的按键重新排列变化等多重阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆