gpt4 book ai didi

ios - 如何对从服务器获取的数据进行排序

转载 作者:行者123 更新时间:2023-11-29 00:17:40 24 4
gpt4 key购买 nike

我从 api 获取一些数据。我必须对日期进行排序。让我在一个数组中给出示例,其中 10 个字典,其中我有一个名为开始时间的参数。我必须显示迟到的事件首先流向下一个即将发生的事件,就像今天一样首先是事件,然后是明天的事件。

我经历了很多我无法理解的例子,有人可以帮助我。

这是来自 api 的结果。

(
{
bookingTime = "<null>";
clubAddress = "Grand by GRT Hotels, Lobby Level, Sir Thyagaraya Road, Thyagaraya Nagar , Chennai";
clubCity = "Chennai,India";
clubFacilities = "<null>";
clubInfoID = 270;
clubLatitude = "13.0402612";
clubLoc = Chennai;
clubLongitude = "80.2423";
clubMobileNumber = "<null>";
clubName = "The Code-Grand by GRT Hotels";
clubType = "Night club";
clubberMobile = "<null>";
clubberName = "<null>";
count = 0;
coupleCover = 0;
coupleEntry = 0;
couplePayment = "<null>";
description = "<null>";
distance = 0;
djId = "<null>";
dressCode = "<null>";
endDate = "2017-07-03 18:00:00";
entryCancellationTime = "<null>";
eventOffer = "<null>";
eventType = DANCE;
favoriteType = NO;
festivalList = "<null>";
"idclub_event" = 6042;
moodDJ = "<null>";
moodMusic = "Salsa, Bachata, Kizomba";
moodTheme = "LATIN CODE NIGHT";
offerCredits = 0;
paidStatus = NOTPAID;
personAllowed = 0;
pictureType = IMAGE;
rateAmbience = 0;
rateAvg = 0;
rateFood = 0;
rateMusic = 0;
rateService = 0;
rsvpEntryTime = "<null>";
rsvpRequestTime = "<null>";
rsvpStatus = NO;
rsvpType = BOTH;
stagCover = 0;
stagEntry = 0;
stagPayment = "<null>";
startDate = "2017-07-03 15:30:00";
stopEdit = "<null>";
},
{
bookingTime = "<null>";
clubAddress = "309 Rajiv Gandhi Salai (OMR), Sholinganallur, Chennai, Tamil Nadu 600119";
clubCity = "Chennai,India";
clubFacilities = "<null>";
clubInfoID = 276;
clubLatitude = "12.9056392";
clubLoc = Sholinganallur;
clubLongitude = "80.2256728";
clubMobileNumber = "<null>";
clubName = "The Gateway Hotel";
clubType = Pub;
clubberMobile = "<null>";
clubberName = "<null>";
count = 0;
coupleCover = 0;
coupleEntry = 0;
couplePayment = "<null>";
description = "<null>";
distance = 0;
djId = "<null>";
dressCode = "<null>";
endDate = "2017-09-23 16:00:00";
entryCancellationTime = "<null>";
eventOffer = "<null>";
eventType = DANCE;
favoriteType = NO;
festivalList = "<null>";
"idclub_event" = 5945;
moodDJ = "<null>";
moodMusic = "Bachata ";
moodTheme = "Toke D Keda: Bachata Concert";
offerCredits = 0;
paidStatus = NOTPAID;
personAllowed = 0;
pictureType = IMAGE;
rateAmbience = 0;
rateAvg = 0;
rateFood = 0;
rateMusic = 0;
rateService = 0;
rsvpEntryTime = "<null>";
rsvpRequestTime = "<null>";
rsvpStatus = NO;
rsvpType = BOTH;
stagCover = 0;
stagEntry = 0;
stagPayment = "<null>";
startDate = "2017-09-23 10:00:00";
stopEdit = "<null>";
},

请帮我解决这个问题,谢谢快速回复。

最佳答案

解析 API 响应并将事件详细信息存储在模型类中。最后你会得到 Events

数组

然后使用NSSortDescriptor对数组进行排序

//startDate key is the variable name of your `Event` model. Where you stored the event dates
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES];

// ascending YES/NO based on your needs

NSArray *sortedEventsList = [arrayOfEvents sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

按日期对字典排序

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSMutableArray *dateArray = [[NSMutableArray alloc] init];
for (int i=0; i<list.count; i++) {
NSMutableDictionary *dict = [list[i] mutableCopy];
NSString *datestr=dict[@"startDate"];
NSDate *date = [formatter dateFromString:datestr];

dict[@"startDate"] = date;
dateArray[i] = dict;
}
NSLog(@"%@",dateArray);
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES];

关于ios - 如何对从服务器获取的数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44890831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com