Sort Dictionary by keys
let dictionary = [
"A" : [1, 2],
"Z" : [3, 4],
"D" : [5, 6]
]
let sortedKeys = Array(dictionary.keys).sorted(<) // ["A", "D", "Z"]
EDIT:
The sorted array from the above code contains keys only, while values have to be retrieved from the original dictionary. However, 'Dictionary'
is also a 'CollectionType'
of (key, value) pairs and we can use the global 'sorted'
function to get a sorted array containg both keys and values, like this:
let sortedKeysAndValues = sorted(dictionary) { $0.0 < $1.0 }
println(sortedKeysAndValues) // [(A, [1, 2]), (D, [5, 6]), (Z, [3, 4])]
EDIT2: The monthly changing Swift syntax currently prefers
let sortedKeys = Array(dictionary.keys).sort(<) // ["A", "D", "Z"]
The global sorted
is deprecated.
Sort dictionary by keys in Swift
Dictionary
already has a sorted
method that takes a closure that defines the sorting function. You can sort by keys in the closure, then simply map over the resulting tuples to get the values only.
let sortedDictKeys = dict.sorted(by: { $0.key < $1.key }).map(\.value)
Sort a Dictionary in Swift
The output of sorted
function above is an Array. So you cannot get keys & values like a Dictionary. But you can use map
function to retrieve those sorted keys & values
Return an Array containing the sorted elements of
source
{according}.
The sorting algorithm is not stable (can change the relative order of
elements for which isOrderedBefore does not establish an order).
let codeValueDict = ["us": "$", "it": "€", "fr": "€"]
let sortedArray = sorted(codeValueDict, {$0.0 < $1.0})
print(sortedArray)
let keys = sortedArray.map {return $0.0 }
print(keys)
let values = sortedArray.map {return $0.1 }
print(values)
Sort Dictionary by values in Swift
Try:
let dict = ["a":1, "c":3, "b":2]
extension Dictionary {
func sortedKeys(isOrderedBefore:(Key,Key) -> Bool) -> [Key] {
return Array(self.keys).sort(isOrderedBefore)
}
// Slower because of a lot of lookups, but probably takes less memory (this is equivalent to Pascals answer in an generic extension)
func sortedKeysByValue(isOrderedBefore:(Value, Value) -> Bool) -> [Key] {
return sortedKeys {
isOrderedBefore(self[$0]!, self[$1]!)
}
}
// Faster because of no lookups, may take more memory because of duplicating contents
func keysSortedByValue(isOrderedBefore:(Value, Value) -> Bool) -> [Key] {
return Array(self)
.sort() {
let (_, lv) = $0
let (_, rv) = $1
return isOrderedBefore(lv, rv)
}
.map {
let (k, _) = $0
return k
}
}
}
dict.keysSortedByValue(<)
dict.keysSortedByValue(>)
Updated:
Updated to the new array syntax and sort semantics from beta 3. Note that I'm using sort
and not sorted
to minimize array copying. The code could be made more compact, by looking at the earlier version and replacing sort
with sorted
and fixing the KeyType[]
to be [KeyType]
Updated to Swift 2.2:
Changed types from KeyType
to Key
and ValueType
to Value
. Used new sort
builtin to Array
instead of sort(Array)
Note performance of all of these could be slightly improved by using sortInPlace
instead of sort
Swift: How to sort a dictionary by value, but the value is a tuple?
Dictionaries are not ordered and can't be sorted directly but you can sort the content which will give you an array of key value tuples. Then this array can be mapped to an array of keys which can be used to access the dictionary in a sorted fashion.
This will sort by the second value in the tuple and return an array of keys
let sorttedKeys = dict.sorted(by: { $0.value.1 < $1.value.1}).map {$0.key}
sorttedKeys.forEach {
print("\($0): \(dict[$0]!)")
}
b: (9, 1)
a: (3, 2)
c: (4, 3)
Sort dictionary by value in swift
In the comments, you specified that you want to sort the value arrays. You can accomplish this using mapValues
and sorted
like this:
let sortedResult = plistdata.mapValues { $0.sorted() }
Note: Dictionaries in Swift are unordered (have no specified order), so it isn't possible to put the keys into a specific order. If you'd like to display the keys in a specific sorted order, then sort them into a separate array using let sortedKeys = plistdata.keys.sorted()
.
How to sort Dictionary by keys where values are array of objects in Swift 4?
struct Name: CustomStringConvertible {
let id: Int
let name: String
let native: String
let meaning: String
let origin: String
let isFavorite: Bool
let gender: String
var description: String {
return "Id: " + String(id) + " - Name: " + name
}
}
let name1 = Name(id: 1, name: "Tim Cook", native: "native", meaning: "meaning", origin: "origin", isFavorite: true, gender: "Male")
let name2 = Name(id: 2, name: "Steve Jobs", native: "native", meaning: "meaning", origin: "origin", isFavorite: true, gender: "Male")
let name3 = Name(id: 3, name: "Tiger Woods", native: "native", meaning: "meaning", origin: "origin", isFavorite: true, gender: "Male")
let name4 = Name(id: 4, name: "Socrates", native: "native", meaning: "meaning", origin: "origin", isFavorite: true, gender: "Male")
let names = [name1, name2, name3, name4]
let dictionary = names.sorted(by: {$0.name < $1.name }).reduce(into: [String: [Name]]()) { result, element in
// make sure there is at least one letter in your string else return
guard let first = element.name.first else { return }
// create a string with that initial
let initial = String(first)
// initialize an array with one element or add another element to the existing value
result[initial, default: []].append(element)
}
let sorted = dictionary.sorted {$0.key < $1.key}
print(sorted) // "[(key: "S", value: [Id: 4 - Name: Socrates, Id: 2 - Name: Steve Jobs]), (key: "T", value: [Id: 3 - Name: Tiger Woods, Id: 1 - Name: Tim Cook])]\n"
Sort Dictionary by Key Value
You need to sort your dictionary values, not your keys. You can create an array of tuples from your dictionary sorting it by its values as follow:
Xcode 9 • Swift 4 or Xcode 8 • Swift 3
let fruitsDict = ["apple": 5, "pear": 9, "grape": 1]
let fruitsTupleArray = fruitsDict.sorted{ $0.value > $1.value }
fruitsTupleArray // [(.0 "pear", .1 9), (.0 "apple", .1 5), (.0 "grape", .1 1)]
for (fruit,votes) in fruitsTupleArray {
print(fruit,votes)
}
fruitsTupleArray.first?.key // "pear"
fruitsTupleArray.first?.value // 9
To sort your dictionary using your keys
let fruitsTupleArray = fruitsDict.sorted{ $0.key > $1.key }
fruitsTupleArray // [(key "pear", value 9), (key "grape", value 1), (key "apple", value 5)]
To sort your dictionary using its keys and localized comparison:
let fruitsTupleArray = fruitsDict.sorted { $0.key.localizedCompare($1.key) == .orderedAscending }
edit/update:
We can also extend Sequence
protocol and implement a custom sort that takes a predicate and sort using a keypath property as long as it conforms to Comparable
:
extension Sequence {
func sorted<T: Comparable>(_ predicate: (Element) -> T, by areInIncreasingOrder: ((T,T)-> Bool) = (<)) -> [Element] {
sorted(by: { areInIncreasingOrder(predicate($0), predicate($1)) })
}
}
Usage:
let sortedFruitsAscending = fruitsDict.sorted(\.value)
print(sortedFruitsAscending)
let sortedFruitsDescending = fruitsDict.sorted(\.value, by: >)
print(sortedFruitsDescending)
This will print
[(key: "grape", value: 1), (key: "apple", value: 5), (key: "pear", value: 9)]
[(key: "pear", value: 9), (key: "apple", value: 5), (key: "grape", value: 1)]
edit/update:
For Xcode 13 or later you can use a new generic structure called KeyPathComparator
:
let fruitsTupleArray = fruitsDict.sorted(using: KeyPathComparator(\.value, order: .reverse))
Swift sort dictionary by value
It is not clear what your expectations are. There is really no such thing as a sorted dictionary. Your code is basically correct except for a misplaced parenthesis. I tried this:
let d = ["Museum1":8785.8971799638,
"Museum2":34420.9643422388,
"Museum3":826.467789130732,
"Museum4":304120.342151219]
for (k,v) in (Array(d).sorted {$0.1 < $1.1}) {
println("\(k):\(v)")
}
Result:
Museum3:826.467789130732
Museum1:8785.8971799638
Museum2:34420.9643422388
Museum4:304120.342151219
If you think that's wrong, you need to explain why.
Related Topics
How to Make Physics Bodies Stick to Nodes Anchor Points
How to Write a Function That Will Unwrap a Generic Property in Swift Assuming It Is an Optional Type
What Does "Get" Mean in a Protocol's Property Declaration
Parameters After Opening Bracket
Spacer Not Working with Form Inside a VStack
Declaration Is Only Valid at File Scope (Extension)
How to Convert Between Related Types Through a Common Initializer
Swift: How to Delete Part of Audio
Argument Labels Do Not Match Any Available Overloads
How to Remove Optional from String Value Swift
Beginner Swift Sprite Kit - Node Collision Detection Help (Skphysicscontact)
Swift Error: Missing Return in a Function Expected to Return 'String'
Convert a Custom Object to Data to Be Saved in Nsuserdefauts
Swift Http Request Use Urlsession
Autolayout Contraints for a View from Xib
Uicollectionviewlayout Not Working with Uiimage in Swift 5 and Xcode 11