这是我的代码。请帮助。
NSDictionary *xmlDict = [NSDictionary dictionaryWithXMLString:Str];
NSArray *fixtureslist1 = [[xmlDict objectForKey:@"Matches"] objectForKey:@"eMatches"];
NSMutableDictionary *dictionaryByDate = [NSMutableDictionary new];
for(NSDictionary *dictionary in fixtureslist1)
{
NSString *dateString = dictionary[@"MatchDate"];
NSMutableArray *arrayWithSameDate = self.resultsSection[dateString];
if(! arrayWithSameDate)
{
arrayWithSameDate = [NSMutableArray new];
self.resultsSection[dateString] = arrayWithSameDate;
}
[arrayWithSameDate addObject: dictionary];
}
//NSLog(@"dictionaryByDate:%@",self.resultsSection);
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
reverseOrder=[[self.resultsSection allKeys] sortedArrayUsingDescriptors:descriptors];
NSLog(@"results:%@",reverseOrder);
答案 0 :(得分:2)
@property (nonatomic, retain) NSMutableDictionary *sections;
在viewDidLoad
中self.sections = [[NSMutableDictionary alloc]init];
[self setupSections];
- (void)setupSections
{
BOOL found;
// Loop through the items and create our keys
for (YourObject *item in self.items)
{
NSString *date= item.Matchdate;
found = NO;
for (NSString *str in [self.sections allKeys])
{
if ([str isEqualToString:date])
{
found = YES;
}
}
if (!found)
{
[self.sections setObject:[[NSMutableArray alloc] init] forKey:date];
}
}
// Loop again and sort the items into their respective keys
for (YourObject *item in self.items)
{
[[self.sections objectForKey:item.MatchDate addObject:item];
}
for (NSString *key in [self.sections allKeys])
{
[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@“MatchDate" ascending:NO]]];
}
//Reload tableView by using self.sections
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.sections allKeys] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionHeader = @"";
if(!isFiltered)
{
sectionHeader = [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}
return sectionHeader;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount;
rowCount = [[self.sections objectForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITAbleViewCellalloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
YourObject *item = [[self.sections objectForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
return cell;
}
答案 1 :(得分:0)
首先,您必须解析xml响应,并生成字典数组。然后您可以使用以下代码进行排序
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [arrayOfDictionaries sortedArrayUsingDescriptors:sortDescriptors];