如何根据另一个数组的排序顺序对多个数组进行排序

时间:2015-04-03 12:59:18

标签: arrays function swift indexing

我有多个数组,我想根据其中一个数组的排序顺序对所有数组进行排序,如下所示:

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

2 个答案:

答案 0 :(得分: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"]]

如果你想在任何类型的集合上创建这个泛型(但仍然返回一个数组,与std lib集合algos的样式相同):

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

答案 1 :(得分:-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]

编辑:我的不好,您还想在此过程中更改其他数组索引。如果需要,我会稍后编辑。