4

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!

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
Emre BEGEN
  • 577
  • 1
  • 7
  • 17
  • Is this the whole data or sample ? – Malav Soni May 04 '15 at 11:58
  • Dictionaries aren't ordered, however you could could have a look at this tutorial on creating your own ordered dictionary: http://timekl.com/blog/2014/06/02/learning-swift-ordered-dictionaries/ – ABakerSmith May 04 '15 at 12:12
  • @malav-soni this is sample data. My dictionary have 964 element like <"11042015",Loto>. So key is a string value is loto object. And i want to sort bye "hafta" property ob Loto object. – Emre BEGEN May 04 '15 at 12:50
  • @abakersmith thanks for shared link, i'll try as soon as possible. May be hash keys help me. – Emre BEGEN May 04 '15 at 12:50

3 Answers3

10

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]

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
3

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 }
Stefan Salatic
  • 4,513
  • 3
  • 22
  • 30
1

You can't. The keys of a dictionary are not sorted. You can get an array of the values and sort that array.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50