1
我使用NSSortDescriptor
排序自定義對象與鍵bandName
,但我沒有得到輸出按字母順序。我如何解決這個問題?問題與NSSortDescriptor。我排序的升序(字母順序)
-(void)getdetails
{
NSLog(@"in getdetail method");
SongRequestAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context1 = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"AddToFav" inManagedObjectContext:context1];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error;
NSMutableArray *array_new=[[NSMutableArray alloc]init];
[array_new addObjectsFromArray:[context1 executeFetchRequest:request error:&error]];
//[self.fetchedObjects addObjectsFromArray:[context1 executeFetchRequest:request error:&error]];
[request release];
**NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"bandName" ascending:YES] autorelease];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
//self.fetchedObjects=
[array_new sortedArrayUsingDescriptors:sortDescriptors];
[self.fetchedObjects addObjectsFromArray:array_new];
[array_new release];**
for (int i=0; i<[self.fetchedObjects count];i++)
{
NSManagedObject
*addfav=[self.fetchedObjects objectAtIndex:i];
addFavObject *addfavobject=[[addFavObject alloc]init];
addfavobject.bandImagePath=[addfav valueForKey:@"bandImagePath"];
addfavobject.bandId=[addfav valueForKey:@"bandId"];
addfavobject.bandName=[addfav valueForKey:@"bandName"];
addfavobject.songId=[addfav valueForKey:@"songId"];
addfavobject.songListId=[addfav valueForKey:@"songListId"];
addfavobject.songName=[addfav valueForKey:@"songName"];
addfavobject.bandRealName=[addfav valueForKey:@"bandRealName"];
addfavobject.biography=[addfav valueForKey:@"biography"];
NSLog(@"addto fav object biography is %@",addfavobject.biography);
[self.addTofavObjectArray addObject:addfavobject];
[addfavobject release];
}
//NSArray *reverse = [[self.addTofavObjectArray reverseObjectEnumerator] allObjects];
// [self.addTofavObjectArray removeAllObjects];
// [self.addTofavObjectArray addObjectsFromArray:reverse];
NSLog(@"self.addTofavObjectArray is %@",self.addTofavObjectArray);
NSLog(@"FETCHED OBJECTS count is %d",[self.fetchedObjects count]);
}
hi mccygnus.thanks for your response it is working ......... – girish 2011-04-30 11:07:25