2015-04-03 40 views
1

我有多个阵列,我想排序所有这些基于其中之一的排序顺序上,像这样:如何基于另一个数组的排序顺序上我有点多个阵列

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"] 

回答

6

您可以通过首先由值进行排序键控数组索引数组他们的索引,然后生成基于这些分类指数新的阵列,利用这样做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) { $0.0 && ($0.1.count == keyArray.count)}, 
     "Arrays all need to be the same length") 


    let permutation = sorted(indices(keyArray)) { 
     keyArray[$0] < keyArray[$1] 
    } 

    return valuesArrays.map { 
     Array(PermutationGenerator(elements: $0, 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[$0] < key[$1] 
    } 

    return map(values) { 
     Array(PermutationGenerator(elements: $0, 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[$0],key[$1]) 
    } 

    return map(values) { 
     Array(PermutationGenerator(elements: $0, indices: permutation)) 
    } 
} 


sortByKeyingCollection(myArr, [myArr2, myArr3], >) 
sortByKeyingCollection(myArr, [myArr2, myArr3], lexicographicalCompare) 
sortByKeyingCollection(myArr, [myArr2, myArr3]) { dropFirst($0) < dropFirst($1) } 
+0

非常感谢你:) – 2015-04-03 13:29:09

+0

你可以编辑它为Swift 2.1与Int例如1,2,3? – 2016-02-23 18:33:29

-1

从我的理解,你想按字母顺序排列你的数组。如果是这样,则可以使用下面的示例中的一个:

实施例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, {$0 < $1}) 

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] 

编辑:我的坏,你也想改变过程中的其他数组索引。如果需要,我会稍后再编辑。

相关问题