-2

Thanks for taking the time to look at the question.

I am trying to sort a Dictionary of Arrays in descending order, please see the data model below. I cannot use any of the sort methods on this array because the dictionary is nested inside another array.

var data = [[22 : [24 : 2, 35 : 3]], [21 : [24 : 21, 72 : 26 ]], [23 : [43 : 24, 53 : 12]]]

The sort methods don't recognize the dictionary as they are nested. Any thoughts?

var sortedKeysAndValues = Array(data.keys).sorted(<)

This throws an error: Value of type '[[Int : [Int: Int]]]' has no member 'keys'

data.sorted(by: { $0.0 < $1.0 })

This throws an error: Unable to infer closure type in the current context

Data modal

[22 : [24 : 2, 35 : 3]]

The Key is 22. Which holds the value of another Dictionary Arrays [Int: Int, Int: Int]

Ideally want the results to be sorted by the key i.e 24, 23, and 22.

Much appreciate the help.

  • Does this answer your question? [Sort Dictionary by keys](https://stackoverflow.com/questions/25377177/sort-dictionary-by-keys) – New Dev Jul 23 '20 at 01:56
  • No. If you look closer at my data model you will see that the dictionary is nested inside of another array. – CocoLocoSwift Jul 23 '20 at 02:12
  • how many keys can each dictionary element could have, and what's the logic then? – New Dev Jul 23 '20 at 02:16
  • There is only one key for each dictionary element. Let me add more info on that to the question. – CocoLocoSwift Jul 23 '20 at 02:17
  • Added more info let me know if that helps – CocoLocoSwift Jul 23 '20 at 02:20
  • You didn't answer my question... Could a dictionary, which is an element of the array, have multiple keys, or none? And if so, what's the sorting logic? – New Dev Jul 23 '20 at 02:26
  • The dictionary which is an element of the array has 1 Int key and the sorting logic is just descending based on the Int key... The only issue I have is, the dictionary is nested inside of an another array so I cannot use any of the sort methods directly. – CocoLocoSwift Jul 23 '20 at 02:28

1 Answers1

0

TL;DR

let asc = data.sorted(by: { $0.keys.first ?? Int.min < $1.keys.first ?? Int.min })

The general approach to sorting any array is to appropriately compare two arbitrary values within that array based on whatever property you want. Between the first and second, return true if first should be order before the second, based on some property of each.

In this case, each element is a dictionary of type: [Int: [Int: Int]].

So, given two dictionaries, which property do use to compare?

Generally speaking, a dictionary could have zero to N keys, so you need to decide which key (or a combination of keys) you'd want to use in the sorting logic.

For your specific case, you could just use the first one (and assume that it always exists), by doing dict.keys.first! (forced unwrapped since it's an optional):

let asc  = data.sorted(by: { a, b in a.keys.first! < b.keys.first! })
let desc = data.sorted(by: { a, b in a.keys.first! > b.keys.first! })

But, using forced unwrapping isn't a good practice, so best to use nil-coalescing by deciding how you want to handle (hypothetical) empty dictionaries, e.g. with Int.min for empty dictionaries first:

let asc = data.sorted(by: { a, b in a.keys.first ?? Int.min < b.keys.first ?? Int.min }) 
New Dev
  • 48,427
  • 12
  • 87
  • 129
  • Amazing! Thank you. That worked. I didn't consider the "keys. first", when trying to solve this. Thanks for giving more information on the sorting. – CocoLocoSwift Jul 23 '20 at 13:09