我在我的应用中将数组作为响应。现在我想根据每个对象的价格对这个数组进行排序,那么我该怎么做呢?在我的应用程序中,我以这种方式得到回复。
请查看以下回复以获取更多信息。
`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";
})
现在我想根据每个对象值价格以降序错误对此数组进行排序,以便根据它如何按降序对其进行排序。
答案 0 :(得分:6)
<强>目标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])
答案 1 :(得分:1)
从响应中创建对象数组时,可以使用sortInPlace
方法对其进行排序,如下所示:
struct SomeObject {
...
let price: Int
...
}
// Create array from response
var array: [SomeObject] = ...
// Sort it
array.sortInPlace { $0.price > $1.price }