当前位置: 首页 > 知识库问答 >
问题:

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

郎意
2023-03-14

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

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

共有1个答案

薛保臣
2023-03-14

您可以通过以下方法来实现:首先按照键控数组索引的值对其进行排序,然后使用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库集合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) }
 类似资料:
  • 我有多个数组,我想根据其中一个数组的排序顺序对所有数组进行排序,如下所示: 我预计函数执行后的数组将如下所示:

  • 问题内容: 是否可以对看起来像这样的数组进行排序和重新排列: 匹配此数组的安排: 不幸的是,我没有任何要跟踪的ID。我将需要优先处理items-array,以使其尽可能接近sortingArr。 更新: 这是我正在寻找的输出: 任何想法如何做到这一点? 问题答案: 就像是: 这是一个较短的代码,但是会破坏数组:

  • 在 Java 中,如何根据另一个排序数组的索引顺序对数组进行排序?例如,如果我有: 我按升序对 arr2 进行排序 我希望另一个是: 我怎么能做到这是Java?我知道我会保存新的排序数组到新的实例。任何帮助,谢谢!

  • 我有两个整数数组,我试图根据另一个数组对第一个数组进行排序。 例如。和 b = {1,2,2,0,0, 在 B 中排序的值是 A 中每个整数的实值 排序后我期望的预期结果是: 这是我用的代码 它给出了我的输出:<code>a={2,3,1,0,0,6}和

  • 问题内容: 我在找出根据一个列表的排序顺序对多个列表进行排序的最佳方法时遇到了麻烦。当前,列表是根据其索引排序的。离职时间列表以(00:00 AM / PM)格式保存时间字符串。它们是这样初始化的: 我需要根据发车时间ArrayList中发车时间的排序顺序对所有列表进行排序。在不更改结果数据结构的情况下对这些列表进行排序的最佳方法是什么。任何协助将不胜感激。 谢谢, 马特 问题答案: 如评论中所述