如何根据另一个数组的排序顺序对多个数组进行排序
How can I sort multiple arrays based on the sorted order of another array
我有多个数组,我想根据其中一个的排序顺序对所有数组进行排序,如下所示:
var myArr = ["b", "a", "c"]
var myArr2 = ["letter b", "letter a", "letter c"]
var myArr3 = ["b is the second letter", "a is the first letter", "c is the third letter"]
func sortMultipleArraysBasedOnOne(alphabeticallyArray:Array, arrays:[Array]){
//order myArr alphabetically
for array in arrays{
//change all arrays indexes like in myArr
}
}
sortMultipleArraysBasedOnOne(myArr, [myArr2, myArr3])
我预计函数执行后数组将是这样的:
myArr = ["a", "b", "c"]
myArr2 = ["letter a", "letter b", "letter c"]
myArr3 = ["a is the first letter", "b is the second letter", "c is the third letter"]
您可以通过首先根据索引值对键控数组索引的数组进行排序,然后使用 PermutationGenerator
:
根据这些排序的索引生成新数组来完成此操作
let myArr = ["b", "a", "c"]
let myArr2 = ["letter b", "letter a", "letter c"]
let myArr3 = ["b is the second letter", "a is the first letter", "c is the third letter"]
func sortByKeyArray(keyArray: [String], valuesArrays: [[String]]) -> [[String]] {
precondition(reduce(valuesArrays, true) { [=10=].0 && ([=10=].1.count == keyArray.count)},
"Arrays all need to be the same length")
let permutation = sorted(indices(keyArray)) {
keyArray[[=10=]] < keyArray[]
}
return valuesArrays.map {
Array(PermutationGenerator(elements: [=10=], indices: permutation))
}
}
sortByKeyArray(myArr, [myArr2, myArr3])
// returns [["letter a", "letter b", "letter c"], ["a is the first letter", "b is the second letter", "c is the third letter"]]
如果你想在任何类型的集合上使用这个泛型(但仍然返回一个数组,与标准库集合算法的风格相同):
func sortByKeyingCollection<C: CollectionType, D: SequenceType
where D.Generator.Element == C,
C.Index: RandomAccessIndexType,
C.Generator.Element: Comparable>
(key: C, values: D) -> [[C.Generator.Element]] {
let permutation = sorted(indices(key)) {
key[[=11=]] < key[]
}
return map(values) {
Array(PermutationGenerator(elements: [=11=], indices: permutation))
}
}
以及采用自定义比较器的版本:
func sortByKeyingCollection<C: CollectionType, D: SequenceType where D.Generator.Element == C, C.Index: RandomAccessIndexType>(key: C, values: D, isOrderedBefore: (C.Generator.Element,C.Generator.Element)->Bool) -> [[C.Generator.Element]] {
let permutation = sorted(indices(key)) {
isOrderedBefore(key[[=12=]],key[])
}
return map(values) {
Array(PermutationGenerator(elements: [=12=], indices: permutation))
}
}
sortByKeyingCollection(myArr, [myArr2, myArr3], >)
sortByKeyingCollection(myArr, [myArr2, myArr3], lexicographicalCompare)
sortByKeyingCollection(myArr, [myArr2, myArr3]) { dropFirst([=12=]) < dropFirst() }
据我了解,您想按字母顺序排列数组。如果是这样,您可以使用以下示例之一:
示例 1
var anArray1 = ["b","a","d","e","c"]
func alphabeticallyOrder(lt : String, rt: String) -> Bool {
return lt < rt
}
anArray1 = sorted(anArray1, alphabeticallyOrder)
println(anArray1) // [a, b, c, d, e]
示例 2
var anArray2 = ["b","a","d","e","c"]
anArray2 = sorted(anArray2, {[=11=] < })
println(anArray2) // [a, b, c, d, e]
示例 3
var anArray3 = ["b","a","d","e","c"]
anArray3 = sorted (anArray3 , <)
println(anArray3) // [a, b, c, d, e]
编辑:糟糕,您还想在此过程中更改其他数组索引。如果需要,我稍后会编辑。
我有多个数组,我想根据其中一个的排序顺序对所有数组进行排序,如下所示:
var myArr = ["b", "a", "c"]
var myArr2 = ["letter b", "letter a", "letter c"]
var myArr3 = ["b is the second letter", "a is the first letter", "c is the third letter"]
func sortMultipleArraysBasedOnOne(alphabeticallyArray:Array, arrays:[Array]){
//order myArr alphabetically
for array in arrays{
//change all arrays indexes like in myArr
}
}
sortMultipleArraysBasedOnOne(myArr, [myArr2, myArr3])
我预计函数执行后数组将是这样的:
myArr = ["a", "b", "c"]
myArr2 = ["letter a", "letter b", "letter c"]
myArr3 = ["a is the first letter", "b is the second letter", "c is the third letter"]
您可以通过首先根据索引值对键控数组索引的数组进行排序,然后使用 PermutationGenerator
:
let myArr = ["b", "a", "c"]
let myArr2 = ["letter b", "letter a", "letter c"]
let myArr3 = ["b is the second letter", "a is the first letter", "c is the third letter"]
func sortByKeyArray(keyArray: [String], valuesArrays: [[String]]) -> [[String]] {
precondition(reduce(valuesArrays, true) { [=10=].0 && ([=10=].1.count == keyArray.count)},
"Arrays all need to be the same length")
let permutation = sorted(indices(keyArray)) {
keyArray[[=10=]] < keyArray[]
}
return valuesArrays.map {
Array(PermutationGenerator(elements: [=10=], indices: permutation))
}
}
sortByKeyArray(myArr, [myArr2, myArr3])
// returns [["letter a", "letter b", "letter c"], ["a is the first letter", "b is the second letter", "c is the third letter"]]
如果你想在任何类型的集合上使用这个泛型(但仍然返回一个数组,与标准库集合算法的风格相同):
func sortByKeyingCollection<C: CollectionType, D: SequenceType
where D.Generator.Element == C,
C.Index: RandomAccessIndexType,
C.Generator.Element: Comparable>
(key: C, values: D) -> [[C.Generator.Element]] {
let permutation = sorted(indices(key)) {
key[[=11=]] < key[]
}
return map(values) {
Array(PermutationGenerator(elements: [=11=], indices: permutation))
}
}
以及采用自定义比较器的版本:
func sortByKeyingCollection<C: CollectionType, D: SequenceType where D.Generator.Element == C, C.Index: RandomAccessIndexType>(key: C, values: D, isOrderedBefore: (C.Generator.Element,C.Generator.Element)->Bool) -> [[C.Generator.Element]] {
let permutation = sorted(indices(key)) {
isOrderedBefore(key[[=12=]],key[])
}
return map(values) {
Array(PermutationGenerator(elements: [=12=], indices: permutation))
}
}
sortByKeyingCollection(myArr, [myArr2, myArr3], >)
sortByKeyingCollection(myArr, [myArr2, myArr3], lexicographicalCompare)
sortByKeyingCollection(myArr, [myArr2, myArr3]) { dropFirst([=12=]) < dropFirst() }
据我了解,您想按字母顺序排列数组。如果是这样,您可以使用以下示例之一:
示例 1
var anArray1 = ["b","a","d","e","c"]
func alphabeticallyOrder(lt : String, rt: String) -> Bool {
return lt < rt
}
anArray1 = sorted(anArray1, alphabeticallyOrder)
println(anArray1) // [a, b, c, d, e]
示例 2
var anArray2 = ["b","a","d","e","c"]
anArray2 = sorted(anArray2, {[=11=] < })
println(anArray2) // [a, b, c, d, e]
示例 3
var anArray3 = ["b","a","d","e","c"]
anArray3 = sorted (anArray3 , <)
println(anArray3) // [a, b, c, d, e]
编辑:糟糕,您还想在此过程中更改其他数组索引。如果需要,我稍后会编辑。