Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何对数组进行降序排序?_Ios_Objective C_Iphone_Swift - Fatal编程技术网

Ios 如何对数组进行降序排序?

Ios 如何对数组进行降序排序?,ios,objective-c,iphone,swift,Ios,Objective C,Iphone,Swift,我在我的应用程序中得到数组作为响应。现在我想根据每个对象的价格对这个数组进行排序,那个么我该怎么做呢?在我的应用程序中,我得到了这样的响应 有关更多信息,请查看以下回复 `get_available_busdetail" = ( { FirmaNo = 284; IslemTipi = 1; "company_logo_url" = "aHR0cHM6Ly9ldGlja2V0LmlwZWt0ci5jb20vd3Nib

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

有关更多信息,请查看以下回复

`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]];
Swift

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 }

谢谢,伙计,它工作得很好,你节省了我这么多时间\