Sie sortieren, aber das Ergebnis zurück zu imagedata
wie diese nicht speichern kann, weil Array(imagedata).sort({ $0.0 < $1.0 })
ein (sortiert) array
erzeugt, während imagedata
den Typ eines dictionary
hat.
Diese Sie einen Kompilierungsfehler geben: imagedata = Array(imagedata).sort({ $0.0 < $1.0 })
Während dies nicht Array(imagedata).sort({ $0.0 < $1.0 })
Dies sagt uns, dass das Problem nicht die Sortiermethode ist, aber die Variable, die wir das Ergebnis speichern verwenden .
Wörterbücher haben ihre eigene sort
Methode, die Sie verwenden können. Aber versuchen, einen array
(der Wert, der von einer beliebigen sort
-Methode zurückgegeben wird) zu dictionary
() zuzuweisen, wird immer fehlschlagen.
EDIT:
Hier ist ein Beispiel dafür, wie Sie die sort
Methode auf Ihrem Wörterbuch benutzen konnte und mit dem Rückgabewert arbeiten:
var imagedata: [String: [String: Any]] = [:]
imagedata["a"] = ["Hello": 2]
imagedata["b"] = ["World": UIImage()]
imagedata["c"] = ["Foo": "Bar"]
imagedata["d"] = ["LifeQuest": 42]
print(imagedata)
// prints the content of the dictionary. Dictionaries are un-ordered, so you may always get different results here
// ["b": ["World": <UIImage: 0x60000009ea00>, {0, 0}], "a": ["Hello": 2], "d": ["LifeQuest": 42], "c": ["Foo": "Bar"]]
let orderedKeysAndValues = imagedata.sort({ $0.0 < $1.0 })
for (key, value) in orderedKeysAndValues {
print("key: \(key)")
print("value: \(value)")
}
// the content of the loop will print
// key: a
// value: ["Hello": 2]
// key: b
// value: ["World": <UIImage: 0x60000009ba80>, {0, 0}]
// key: c
// value: ["Foo": "Bar"]
// key: d
// value: ["LifeQuest": 42]
Wie ist die Datenstruktur der 'imagedata' Variablen in' Array (imagedata) '? – Enix
Ein Wörterbuch ist per Definition ungeordnet und das Ergebnis Ihrer "gefälschten Sortierung" ist ein Array von Tupeln anstatt eines Wörterbuchs. – vadian
Dies ist imagedata: ["10October201611": ["images": [, {500, 500}, , {1024, 768}], "Anzahl": 2, "Datum" : "Montag: 10-Oktober-2016-11: 14: 48" ", ..." 10Oktober201608 ": [" images ": [, {1024, 768}, , { 1024, 768}], "count": 2, "date": "Montag: 10-Oktober-2016-08: 18: 52PM"]] –
ConfusedLuigi