如何以降序方式对数组进行排序



我在应用程序中获得数组作为响应。现在我想根据每个对象的价格对这个数组进行排序,那么我该怎么做呢?在我的应用程序中,我以这种方式获得响应。

有关详细信息,请参阅以下回复。

`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";
    })

现在我想根据每个对象值 Price 以降序错误对这个数组进行排序,那么基于它如何按降序对它进行排序。

Objective-C

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

迅速

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

当你从响应创建对象数组时,你可以使用sortInPlace方法对其进行排序,如下所示:

struct SomeObject {
    ...
    let price: Int
    ...
}
// Create array from response
var array: [SomeObject] = ... 
// Sort it
array.sortInPlace { $0.price > $1.price }

最新更新