Arrays Swift:按键或值对字典进行排序,并返回(键或值的)有序数组

Arrays Swift:按键或值对字典进行排序,并返回(键或值的)有序数组,arrays,swift,sorting,dictionary,Arrays,Swift,Sorting,Dictionary,我有一个字典,需要从中派生一个键数组和一个按键或值排序的值数组 我的用例是文件夹列表。字典保存文件夹名称(键)和文件夹中的项目计数(值)。我想按键名(A到Z或Z到A)和计数大小(大到小或小到大)排序 在Swift中对钥匙进行分类很容易。但是我使用迭代来提供排序后的值列表。这似乎不是一种快速的做事方式,但我对Swift中地图/排序/等的理解还不够好,无法让我看到一种更聪明的做事方式 有谁能解释一下实现这一目标的明智而简洁的快速方式吗 我目前的代码是: let dictionary = ["Alph

我有一个字典,需要从中派生一个键数组和一个按键或值排序的值数组

我的用例是文件夹列表。字典保存文件夹名称(键)和文件夹中的项目计数(值)。我想按键名(A到Z或Z到A)和计数大小(大到小或小到大)排序

在Swift中对钥匙进行分类很容易。但是我使用迭代来提供排序后的值列表。这似乎不是一种快速的做事方式,但我对Swift中地图/排序/等的理解还不够好,无法让我看到一种更聪明的做事方式

有谁能解释一下实现这一目标的明智而简洁的快速方式吗

我目前的代码是:

let dictionary = ["Alpha" : 24, "Beta" : 47, "Gamma" : 12, "Delta" : 33]

enum FolderOrder : Int {
    case AlphaAtoZ
    case AlphaZtoA
    case SizeLargeToSmall
    case SizeSmallToLarge
}

func folderList(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [String] {
    switch orderedBy {
    case .AlphaAtoZ:
        return fromDictionary.keys.sort() {$0 < $1}

    case .AlphaZtoA:
        return fromDictionary.keys.sort() {$1 < $0}

    case .SizeSmallToLarge:
        return fromDictionary.keys.sort(){fromDictionary[$0] < fromDictionary [$1]}

    case .SizeLargeToSmall:
        return fromDictionary.keys.sort(){fromDictionary[$1] < fromDictionary [$0]}
    }
}

func folderCounts(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [Int]
{
    let orderedKeys = folderList(fromDictionary, orderedBy: orderedBy)
    var orderedValues = [Int]()

    for key in orderedKeys {
        orderedValues.append(fromDictionary[key] ?? 0)
    }

    return orderedValues
}

folderList(dictionary, orderedBy: .AlphaAtoZ)
// ["Alpha", "Beta", "Delta", "Gamma"]

folderList(dictionary, orderedBy: .AlphaZtoA)
// ["Gamma", "Delta", "Beta", "Alpha"]

folderList(dictionary, orderedBy: .SizeSmallToLarge)
// ["Gamma", "Alpha", "Delta", "Beta"]

folderList(dictionary, orderedBy: .SizeLargeToSmall)
//["Beta", "Delta", "Alpha", "Gamma"]

folderCounts(dictionary, orderedBy: .AlphaAtoZ)
// [24, 47, 33, 12]

folderCounts(dictionary, orderedBy: .SizeLargeToSmall)
// [47, 33, 24, 12]
let dictionary=[“Alpha”:24,“Beta”:47,“Gamma”:12,“Delta”:33]
枚举文件夹顺序:Int{
案例AlphaAtoZ
病例AlphaZtoA
案例大小目标购物中心
案例规模最大
}
func folderList(fromDictionary:[String:Int],OrderBy:FolderOrder=.AlphaAtoZ)->[String]{
开关订购者{
案例:AlphaAtoZ:
从dictionary.keys.sort()返回({$0<$1}
案例:AlphaZtoA:
从dictionary.keys.sort()返回({$1<$0}
案例规模最大:
返回fromDictionary.keys.sort(){fromDictionary[$0][Int]
{
让orderedKeys=folderList(fromDictionary,orderedBy:orderedBy)
var orderedValues=[Int]()
对于orderedKeys中的密钥{
orderedValues.append(从字典[键]??0)
}
返回orderedValues
}
文件夹列表(字典,排序人:.AlphaAtoZ)
//[“α”、“β”、“δ”、“γ”]
文件夹列表(字典,排序人:.AlphaZtoA)
//[“伽马”、“德尔塔”、“贝塔”、“阿尔法”]
folderList(字典,订购者:.SizeSmallToLarge)
//[“伽马”、“阿尔法”、“德尔塔”、“贝塔”]
folderList(字典,订购者:.SizeTargetOSmall)
//[“β”、“δ”、“α”、“γ”]
文件夹计数(字典,排序人:.AlphaAtoZ)
// [24, 47, 33, 12]
folderCounts(字典,订购人:.SizeTargetOSmall)
// [47, 33, 24, 12]
更新 多亏了两个有用的答案,尤其是@nRewik,我简化了代码,提高了对Swift的理解

修订后的代码,注释说明了我最初不清楚的地方,因此可能对其他人有所帮助:

let dictionary = ["Alpha" : 24, "Beta" : 47, "Gamma" : 12, "Delta" : 33]

enum FolderOrder {
    case AlphaAtoZ
    case AlphaZtoA
    case SizeLargeToSmall
    case SizeSmallToLarge
}

func folderListAndCounts(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> ([String], [Int]) {

    var sortedDictionary : [(String, Int)]

    switch orderedBy {

    // The closure when sort() is applied to a dictionary takes two tuples as parameters
    // where the tuples are of the form (key, value). The first tuple can be accessed as $0.
    // Its key can be accessed as $0.0 and its value as $0.1

    case .AlphaAtoZ:
        sortedDictionary = fromDictionary.sort{ $0.0 < $1.0 } // item(n).key < item(n+1).key
    case .AlphaZtoA:
        sortedDictionary = fromDictionary.sort{ $0.0 > $1.0 } // item(n).key > item(n+1).key
    case .SizeSmallToLarge:
        sortedDictionary = fromDictionary.sort{ $0.1 < $1.1 } // item(n).value < item(n+1).value
    case .SizeLargeToSmall:
        sortedDictionary = fromDictionary.sort{ $0.1 > $1.1 } // item(n).value < item(n+1).value
    }

    // The sorted dictionary has the type: [(String, Int)], i.e. it's an array of tuples.
    // The closure when map is applied to an array of tuples is a tuple. The tuple can be
    // accessed as $0. Its key can be accessed as $0.0 and its value as $0.1

    let sortedKeys = sortedDictionary.map{$0.0}
    let sortedValues = sortedDictionary.map{$0.1}

    // Returns a tuple (arrayOfKeys, arrayOfValues)
    return (sortedKeys, sortedValues)
}

let (keys, counts) = folderListAndCounts(dictionary, orderedBy: .SizeSmallToLarge)
let dictionary=[“Alpha”:24,“Beta”:47,“Gamma”:12,“Delta”:33]
枚举文件夹顺序{
案例AlphaAtoZ
病例AlphaZtoA
案例大小目标购物中心
案例规模最大
}
func folderListAndCounts(fromDictionary:[String:Int],orderedBy:FolderOrder=.AlphaAtoZ)->([String],[Int]){
var sortedDictionary:[(字符串,Int)]
开关订购者{
//对字典应用sort()时的闭包采用两个元组作为参数
//其中元组的形式为(键、值)。第一个元组可以作为$0访问。
//它的键可以访问为$0.0,其值可以访问为$0.1
案例:AlphaAtoZ:
sortedDictionary=fromDictionary.sort{$0.0<$1.0}//item(n).key$1.0}//item(n).key>item(n+1).key
案例规模最大:
sortedDictionary=fromDictionary.sort{$0.1<$1.1}//item(n).value$1.1}//item(n).value
这个怎么样?这将更改您的代码,对字典条目进行排序,然后获取键或值

let dictionary = ["Alpha" : 24, "Beta" : 47, "Gamma" : 12, "Delta" : 33]

enum FolderOrder : Int {
  case AlphaAtoZ
  case AlphaZtoA
  case SizeLargeToSmall
  case SizeSmallToLarge
}

func entryList(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [(String, Int)] {
  switch orderedBy {
  case .AlphaAtoZ:
    return fromDictionary.sort { $0.0 < $1.0 }

  case .AlphaZtoA:
    return fromDictionary.sort { $0.0 > $1.0 }

  case .SizeSmallToLarge:
    return fromDictionary.sort { $0.1 < $1.1 }

  case .SizeLargeToSmall:
    return fromDictionary.sort { $0.1 > $1.1 }
  }
}

func folderList(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [String] {
  return entryList(fromDictionary, orderedBy: orderedBy).map { $0.0 }
}

func folderCounts(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [Int] {
  return entryList(fromDictionary, orderedBy: orderedBy).map { $0.1 }
}

folderList(dictionary, orderedBy: .AlphaAtoZ)
// ["Alpha", "Beta", "Delta", "Gamma"]

folderList(dictionary, orderedBy: .AlphaZtoA)
// ["Gamma", "Delta", "Beta", "Alpha"]

folderList(dictionary, orderedBy: .SizeSmallToLarge)
// ["Gamma", "Alpha", "Delta", "Beta"]

folderList(dictionary, orderedBy: .SizeLargeToSmall)
//["Beta", "Delta", "Alpha", "Gamma"]

folderCounts(dictionary, orderedBy: .AlphaAtoZ)
// [24, 47, 33, 12]

folderCounts(dictionary, orderedBy: .SizeLargeToSmall)
// [47, 33, 24, 12]
let dictionary=[“Alpha”:24,“Beta”:47,“Gamma”:12,“Delta”:33]
枚举文件夹顺序:Int{
案例AlphaAtoZ
病例AlphaZtoA
案例大小目标购物中心
案例规模最大
}
func entryList(fromdocdictionary:[String:Int],orderedBy:FolderOrder=.AlphaAtoZ)->[(String,Int)]{
开关订购者{
案例:AlphaAtoZ:
从dictionary.sort{$0.0<$1.0}返回
案例:AlphaZtoA:
从dictionary.sort返回{$0.0>$1.0}
案例规模最大:
从dictionary.sort{$0.1<$1.1}返回
案例。SizeTargetOSmall:
从dictionary.sort返回{$0.1>$1.1}
}
}
func folderList(fromDictionary:[String:Int],OrderBy:FolderOrder=.AlphaAtoZ)->[String]{
return entryList(fromdograry,orderedBy:orderedBy).map{$0.0}
}
func folderCounts(fromDictionary:[String:Int],OrderBy:FolderOrder=.AlphaAtoZ)->[Int]{
return entryList(fromdograry,orderedBy:orderedBy).map{$0.1}
}
文件夹列表(字典,排序人:.AlphaAtoZ)
//[“α”、“β”、“δ”、“γ”]
文件夹列表(字典,排序人:.AlphaZtoA)
//[“伽马”、“德尔塔”、“贝塔”、“阿尔法”]
folderList(字典,订购者:.SizeSmallToLarge)
//[“伽马”、“阿尔法”、“德尔塔”、“贝塔”]
folderList(字典,订购者:.SizeTargetOSmall)
//[“β”、“δ”、“α”、“γ”]
文件夹计数(字典,排序人:.AlphaAtoZ)
// [24, 47, 33, 12]
folderCounts(字典,订购人:.SizeTargetOSmall)
// [47, 33, 24, 12]

这个怎么样?这将更改您的代码,对字典条目进行排序,然后获取键或值

let dictionary = ["Alpha" : 24, "Beta" : 47, "Gamma" : 12, "Delta" : 33]

enum FolderOrder : Int {
  case AlphaAtoZ
  case AlphaZtoA
  case SizeLargeToSmall
  case SizeSmallToLarge
}

func entryList(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [(String, Int)] {
  switch orderedBy {
  case .AlphaAtoZ:
    return fromDictionary.sort { $0.0 < $1.0 }

  case .AlphaZtoA:
    return fromDictionary.sort { $0.0 > $1.0 }

  case .SizeSmallToLarge:
    return fromDictionary.sort { $0.1 < $1.1 }

  case .SizeLargeToSmall:
    return fromDictionary.sort { $0.1 > $1.1 }
  }
}

func folderList(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [String] {
  return entryList(fromDictionary, orderedBy: orderedBy).map { $0.0 }
}

func folderCounts(fromDictionary: [String: Int], orderedBy : FolderOrder = .AlphaAtoZ) -> [Int] {
  return entryList(fromDictionary, orderedBy: orderedBy).map { $0.1 }
}

folderList(dictionary, orderedBy: .AlphaAtoZ)
// ["Alpha", "Beta", "Delta", "Gamma"]

folderList(dictionary, orderedBy: .AlphaZtoA)
// ["Gamma", "Delta", "Beta", "Alpha"]

folderList(dictionary, orderedBy: .SizeSmallToLarge)
// ["Gamma", "Alpha", "Delta", "Beta"]

folderList(dictionary, orderedBy: .SizeLargeToSmall)
//["Beta", "Delta", "Alpha", "Gamma"]

folderCounts(dictionary, orderedBy: .AlphaAtoZ)
// [24, 47, 33, 12]

folderCounts(dictionary, orderedBy: .SizeLargeToSmall)
// [47, 33, 24, 12]
let dictionary=[“Alpha”:24,“Beta”:47,“Gamma”:12,“Del”