Reputation: 900
I am using NSSortDescriptor
to sort the custom objects with key bandName
, but I am not getting the output in alphabetical order. How do I fix that?
-(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]);
}
Upvotes: 1
Views: 1084
Reputation: 4215
sortedArrayUsingDescriptors: doesn't sort in place the array it was sent to. It returns a new array with the objects sorted. It should work with:
NSArray *sortedArray = [array_new sortedArrayUsingDescriptors:sortDescriptors];
[self.fetchedObjects addObjectsFromArray:sortedArray];
You don't need to do the sort separately like that though. You can give the sort descriptors to your fetch request and the array returned from executing it will be sorted. I'd highly recommend doing it this way instead of how you're doing it. So you can replace everything before the for loop with:
SongRequestAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context1 = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"AddToFav" inManagedObjectContext:context1];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"bandName" ascending:YES] autorelease];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
[sortDescriptor release];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setSortDescriptors:sortDescriptors];
NSError *error;
NSMutableArray *array_new = [context1 executeFetchRequest:request error:&error];
if (!array_new)
{
NSLog(@"error: %@", error);
}
[self.fetchedObjects addObjectsFromArray:array_new];
[request release];
I also added in the check for array_new to print the error. You might want to add additional error handling there too.
Upvotes: 4