如何在 Swift/Xcode 中对 1 个数组进行排序并通过相同的键更改对多个其他数组重新排序 [英] How to sort 1 array in Swift / Xcode and reorder multiple other arrays by the same keys changes

查看:25
本文介绍了如何在 Swift/Xcode 中对 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.

Swift/Xcode 7.2 中是否有等效的命令?

Is there an equivalent command in Swift / Xcode 7.2?

我目前有一组数组:

名字年龄城市国家活跃

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

这是对技术的改进:

这是相同的方法,但在一个步骤中进行排序和分配.(可以重新分配给原始数组或单独的数组)

It's the same approach but does the sorting and assignment in one step. (can be reassigned to original arrays or to separate ones)

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

和一个 Array 扩展,使其在原位排序时更易于使用:

and an Array extension to make it even easier to use if you are sorting in place:

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,ages,cities,countries,actives) = 
    {( 
       $0.reordered(firstNames), 
       $0.reordered(ages), 
       $0.reordered(cities),
       $0.reordered(countries), 
       $0.reordered(actives) 
    )} 
    (firstNames.ordering(by:<))

这篇关于如何在 Swift/Xcode 中对 1 个数组进行排序并通过相同的键更改对多个其他数组重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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