Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Sort Dictionary by property of an object value

I have a dictionary like <String,Loto> and Loto is object like below;

Loto:   
 {
    "success": true,
    "data": {
    "oid": "64kbbqi8dbxygb00",
    "hafta": 961,
    "buyukIkramiyeKazananIl": "",
    "cekilisTarihi": "11/04/2015",
    "cekilisTuru": "SAYISAL_LOTO",
    "rakamlar": "03#02#48#16#15#08",
    "rakamlarNumaraSirasi": "02 - 03 - 08 - 15 - 16 - 48",
    "devretti": false,
    "devirSayisi": 0,
    "bilenKisiler": [
    {
    "oid": "64kbbxi8dbxyg403",
    "kisiBasinaDusenIkramiye": 7.35,
    "kisiSayisi": 185712,
    "tur": "$3_BILEN"
    },
    {
    "oid": "64kbbxi8dbxyg402",
    "kisiBasinaDusenIkramiye": 53.05,
    "kisiSayisi": 9146,
    "tur": "$4_BILEN"
    },
    {
    "oid": "64kbbxi8dbxyg401",
    "kisiBasinaDusenIkramiye": 4532.2,
    "kisiSayisi": 142,
    "tur": "$5_BILEN"
    },
    {
    "oid": "64kbbxi8dbxyg400",
    "kisiBasinaDusenIkramiye": 1528438.75,
    "kisiSayisi": 1,
    "tur": "$6_BILEN"
    }
    ],
    "buyukIkrKazananIlIlceler": [
    {
    "il": "10",
    "ilView": "BALIKESÄ°R",
    "ilce": "01001",
    "ilceView": "AYVALIK"
    }
    ],
    "kibrisHasilati": 51127,
    "devirTutari": 0.09,
    "kolonSayisi": 10537872,
    "kdv": 1599672.97,
    "toplamHasilat": 10537872,
    "hasilat": 8938199.03,
    "sov": 893819.9,
    "ikramiyeEH": 8044379.129999999,
    "buyukIkramiye": 1528432.03,
    "haftayaDevredenTutar": 0
    }
    }

So my dictionary like <"11042015",Loto> and i want to sort this dictionary by "hafta" property of loto object.

How can i do this? Please help me!

like image 283
Emre BEGEN Avatar asked Dec 09 '22 03:12

Emre BEGEN


2 Answers

If Loto is an object with a hafta property, you can sort your dictionary by passing it into the sorted function, along with a closure that tells it how to order the entries:

sorted(dict) { $0.1.hafta < $1.1.hafta }

($0.1 and $1.1 because dictionaries present as a sequence of key/value pairs - you want to sort by a property of the value i.e. tuple entry 1)

Note, this will give you back a sorted array, of type [(String,Loto)] pairs, rather than a dictionary (as Swift dictionaries are unordered).

(if Loto is not really an object but rather another dictionary, you might need to do {$0.1["hafta"] < $1.1["hafta"]} - it really depends on how you’re holding your data - the good news is you don’t need to worry about optionals, since they can be compared with <)

If you don’t need the keys, just sort the values:

sorted(dict.values) { $0.hafta < $1.hafta }

which will give you back a sorted array of type [Loto]

like image 173
Airspeed Velocity Avatar answered Dec 11 '22 08:12

Airspeed Velocity


Dictionaries are not ordered. What you need is to convert it to array, and then sort it. So something like this.

let lotoArray = lotoDictionary.allObjects as [Loto]

lotoArray.sort { $0.hafta < $1.hafta }
like image 20
Stefan Salatic Avatar answered Dec 11 '22 08:12

Stefan Salatic