Govind Rakholiya
Govind Rakholiya

Reputation: 295

How to sort array in descending manner?

I'm getting array as a response in my app. Now I want to sort this array based on price of each object, so how can I do this? In my app I am getting response in this manner.

Please find the below response for more information.

`get_available_busdetail" =     (
            {
        FirmaNo = 284;
        IslemTipi = 1;
        "company_logo_url" = "aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTI4NA==";
        "company_name" = "Metro Turizm";
        "departure_city" = ANTALYA;
        "destination_city" = "\U0130STANBUL";
        firmaAdi = "Metro Turizm";
        hatNo = 1;
        "info_i" = "ANTALYA ->\U0130STANBUL ";
        price = 75;
        saat = "MTkwMC0wMS0wMVQxMDozMDowMCswMjowMA==";
        seat = "2+1";
        seferTakipNo = 073403;
        tarih = "2016-03-09";
        time = "10:30";
    },
            {
        FirmaNo = 20;
        IslemTipi = 1;
        "company_logo_url" = aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTIw;
        "company_name" = "\U0130smail Ayaz";
        "departure_city" = ANTALYA;
        "destination_city" = "\U0130STANBUL";
        firmaAdi = "\U0130smail Ayaz";
        hatNo = 6;
        "info_i" = "Alanya ->ANTALYA ->\U0130STANBUL ";
        price = 70;
        saat = "MTkwMC0wMS0wMVQxMDowMDowMCswMjowMA==";
        seat = "2+1";
        seferTakipNo = 179680;
        tarih = "2016-03-09";
        time = "13:00";
    },




 {
        FirmaNo = 284;
        IslemTipi = 1;
        "company_logo_url" = "aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib3MzL0xvZ29WZXIuQXNweD9mbnVtPTI4NA==";
        "company_name" = "Metro Turizm";
        "departure_city" = ANTALYA;
        "destination_city" = "\U0130STANBUL";
        firmaAdi = "Metro Turizm";
        hatNo = 1;
        "info_i" = "ANTALYA ->\U0130STANBUL ";
        price = 70;
        saat = "MTkwMC0wMS0wMVQxNjowMDowMCswMjowMA==";
        seat = "2+2";
        seferTakipNo = 073430;
        tarih = "2016-03-09";
        time = "16:00";
    })

Now I want to sort this array in descending error based on each object value Price so based on it how can I sort this in descending order.

Upvotes: 0

Views: 548

Answers (2)

Alexey Pichukov
Alexey Pichukov

Reputation: 3405

When you create array of objects from response, you can sort it with sortInPlace method like this:

struct SomeObject {
    ...
    let price: Int
    ...
}

// Create array from response
var array: [SomeObject] = ... 

// Sort it
array.sortInPlace { $0.price > $1.price }

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82769

Objective-C

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"price"  ascending:NO];
NSArray * sortedArray =[yourArrayName sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

Swift

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "price", ascending: False)
var sortedArray: NSArray = yourArrayName.sortedArrayUsingDescriptors([descriptor])

Upvotes: 6

Related Questions