Swift2 - 根据另一个 INT 数组的排序顺序对多个数组进行排序
Swift2 - Sort multiple arrays based on the sorted order of another INT array
let points:[Int] = [200, 1000, 100, 500]
let people:[String] = ["Harry", "Jerry", "Hannah", "John"]
let peopleIds:[Int] = [1, 2, 3, 4]
let sex:[String] = ["Male", "Male", "Female", "Male"]
如何按点对数组进行排序?:
let points:[Int] = [1000, 500, 200, 100]
let people:[String] = ["Jerry", "John", "Harry", "Hannah"]
let peopleIds:[Int] = [2, 4, 1, 3]
let sex:[String] = ["Male", "Male", "Male", "Female"]
它不是 的副本
我试过这些答案,但没有用
创建一个按您希望的方式排序的新索引数组"descending",然后映射其他数组。
var points:[Int] = [200, 1000, 100, 500]
var people:[String] = ["Harry", "Jerry", "Hannah", "John"]
var peopleIds:[Int] = [1, 2, 3, 4]
var sex:[String] = ["Male", "Male", "Female", "Male"]
//descending order array of indexes
let sortedOrder = points.enumerate().sort({[=10=].1>.1}).map({[=10=].0})
//Map the arrays based on the new sortedOrder
points = sortedOrder.map({points[[=10=]]})
people = sortedOrder.map({people[[=10=]]})
peopleIds = sortedOrder.map({peopleIds[[=10=]]})
sex = sortedOrder.map({sex[[=10=]]})
我刚刚测试了这个解决方案,它运行良好。
这是使用您自己的数据的解决方案。
let points:[Int] = [200, 1000, 100, 500]
let people:[String] = ["Harry", "Jerry", "Hannah", "John"]
let peopleIds:[Int] = [1, 2, 3, 4]
let sex:[String] = ["Male", "Male", "Female", "Male"]
let sortedPointsEnumerateIndexes = points.enumerate().sort { [=10=].element > .element}.map { [=10=].index }
var sortedPoints:[Int] = []
var sortedPeople:[String] = []
var sortedPeopleIds:[Int] = []
var sortedSex:[String] = []
for i in sortedPointsEnumerateIndexes {
sortedPoints.append(points[i])
sortedPeople.append(people[i])
sortedPeopleIds.append(peopleIds[i])
sortedSex.append(sex[i])
}
print(sortedPoints) // [1000, 500, 200, 100]
print(sortedPeople) // ["Jerry", "John", "Harry", "Hannah"]
print(sortedPeopleIds) // [2, 4, 1, 3]
print(sortedSex) // ["Male", "Male", "Male", "Female"]
// with map
let sorted2Point = sortedPointsEnumerateIndexes.map{ points[[=10=]] }
let sorted2People = sortedPointsEnumerateIndexes.map{ people[[=10=]] }
// ....
print(sorted2Point) // [1000, 500, 200, 100]
print(sorted2People) // ["Jerry", "John", "Harry", "Hannah"]
let points:[Int] = [200, 1000, 100, 500]
let people:[String] = ["Harry", "Jerry", "Hannah", "John"]
let peopleIds:[Int] = [1, 2, 3, 4]
let sex:[String] = ["Male", "Male", "Female", "Male"]
如何按点对数组进行排序?:
let points:[Int] = [1000, 500, 200, 100]
let people:[String] = ["Jerry", "John", "Harry", "Hannah"]
let peopleIds:[Int] = [2, 4, 1, 3]
let sex:[String] = ["Male", "Male", "Male", "Female"]
它不是
创建一个按您希望的方式排序的新索引数组"descending",然后映射其他数组。
var points:[Int] = [200, 1000, 100, 500]
var people:[String] = ["Harry", "Jerry", "Hannah", "John"]
var peopleIds:[Int] = [1, 2, 3, 4]
var sex:[String] = ["Male", "Male", "Female", "Male"]
//descending order array of indexes
let sortedOrder = points.enumerate().sort({[=10=].1>.1}).map({[=10=].0})
//Map the arrays based on the new sortedOrder
points = sortedOrder.map({points[[=10=]]})
people = sortedOrder.map({people[[=10=]]})
peopleIds = sortedOrder.map({peopleIds[[=10=]]})
sex = sortedOrder.map({sex[[=10=]]})
我刚刚测试了这个解决方案,它运行良好。
这是使用您自己的数据的解决方案。
let points:[Int] = [200, 1000, 100, 500]
let people:[String] = ["Harry", "Jerry", "Hannah", "John"]
let peopleIds:[Int] = [1, 2, 3, 4]
let sex:[String] = ["Male", "Male", "Female", "Male"]
let sortedPointsEnumerateIndexes = points.enumerate().sort { [=10=].element > .element}.map { [=10=].index }
var sortedPoints:[Int] = []
var sortedPeople:[String] = []
var sortedPeopleIds:[Int] = []
var sortedSex:[String] = []
for i in sortedPointsEnumerateIndexes {
sortedPoints.append(points[i])
sortedPeople.append(people[i])
sortedPeopleIds.append(peopleIds[i])
sortedSex.append(sex[i])
}
print(sortedPoints) // [1000, 500, 200, 100]
print(sortedPeople) // ["Jerry", "John", "Harry", "Hannah"]
print(sortedPeopleIds) // [2, 4, 1, 3]
print(sortedSex) // ["Male", "Male", "Male", "Female"]
// with map
let sorted2Point = sortedPointsEnumerateIndexes.map{ points[[=10=]] }
let sorted2People = sortedPointsEnumerateIndexes.map{ people[[=10=]] }
// ....
print(sorted2Point) // [1000, 500, 200, 100]
print(sorted2People) // ["Jerry", "John", "Harry", "Hannah"]