如何基于另一个数组的排序顺序上我有点多个阵列 [英] How can I sort multiple arrays based on the sorted order of another array

查看:144
本文介绍了如何基于另一个数组的排序顺序上我有点多个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个阵列,我想排序他们都基于他们中的一个,像这样的排序顺序:

  VAR myArr,该= [B,A,C]
VAR myArr2 = [字母B,信了,字母c]
变种myArr3 = [b是第二个字母,一个是第一个字母,c是第三个字母]FUNC sortMultipleArraysBasedOnOne(alphabeticallyArray:数组,数组:[数组]){
  //为了myArr,该字母
  在数组的数组{
    //改变所有的数组索引像myArr,该
  }
}sortMultipleArraysBasedOnOne(myArr,该[myArr2,myArr3])

我函数执行后,预计阵列将是这样的:

  myArr,该=A,B,C]
myArr2 = [字母,字母B,C字母]
myArr3 = [一个是第一个字母,b是第二个字母,c是第三个字母]


解决方案

您可以先的价值观排序键控数组索引数组他们的索引,然后生成基于这些分类指数新阵列,使用<$做到这一点C $ C> PermutationGenerator :

 让myArr,该= [B,A,C]
让myArr2 = [字母B,信了,字母c]
让myArr3 = [b是第二个字母,一个是第一个字母,c是第三个字母]FUNC sortByKeyArray(keyArray:[字符串],values​​Arrays:[字符串]]) - GT; [字符串]] {    precondition(减少(values​​Arrays,真){$ 0.0次和放大器;及($ 0.1.count == keyArray.count)},
        阵列都需要是相同的长度)
    让置换=排序(指数(keyArray)){
        keyArray [$ 0]&LT; keyArray [$ 1]
    }    返回values​​Arrays.map {
        阵列(PermutationGenerator(元素:$ 0,指数:排列))
    }
}sortByKeyArray(myArr,该[myArr2,myArr3])
//返回[字母,字母B,C字母],[一个是第一个字母,b是第二个字母,c是第三个字母]]


如果你想使这个通用的任何类型的集合(但仍返回一个数组,在相同的风格为标准库集合交易算法):

  FUNC sortByKeyingCollection&LT; C:CollectionType,D:序列类型
  其中,D.Generator.Element == C,
        C.Index:RandomAccessIndexType,
        C.Generator.Element:可比&GT;
(键:C,值:D) - GT; [C.Generator.Element]] {    让置换=排序(指数(键)){
        键[$ 0]&LT;键[$ 1]
    }    返回地图(值){
        阵列(PermutationGenerator(元素:$ 0,指数:排列))
    }
}

和一个版本,它接受一个自定义的比较:

  FUNC sortByKeyingCollection&LT; C:CollectionType,D:序列类型,其中D.Generator.Element == C,C.Index:RandomAccessIndexType&GT;(键:C,值:D,isOrderedBefore:( C.Generator.Element,C.Generator.Element) -  GT;布尔) -  GT; [C.Generator.Element]] {    让置换=排序(指数(键)){
        isOrderedBefore(键[$ 0],键[$ 1])
    }    返回地图(值){
        阵列(PermutationGenerator(元素:$ 0,指数:排列))
    }
}
sortByKeyingCollection(myArr,该[myArr2,myArr3]&GT;)
sortByKeyingCollection(myArr,该[myArr2,myArr3],lexicographicalCompare)
sortByKeyingCollection(myArr,该[myArr2,myArr3]){dropFirst($ 0)&下; dropFirst($ 1)}

I have multiple arrays, and I want to sort all of them based on the sorted order of one of them, like so:

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

I expect after function execution the arrays will be like this:

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

解决方案

You can do this by first sorting an array of the keying array’s indices by the values they index, then generating new arrays based on those sorted indices, using 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"]]


If you want to make this generic on any kind of collection (but still returning an array, in the same style as the std lib collection 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))
    }
}

And a version that takes a custom comparator:

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

这篇关于如何基于另一个数组的排序顺序上我有点多个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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