NSSortDescriptor的问题。我按升序排序(按字母顺序排列)

时间:2011-04-30 06:00:56

标签: iphone objective-c core-data nssortdescriptor

我正在使用NSSortDescriptor使用键bandName对自定义对象进行排序,但我没有按字母顺序获取输出。我该如何解决这个问题?

-(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]);  
}

1 个答案:

答案 0 :(得分:4)

sortedArrayUsingDescriptors:不对它发送到的数组进行排序。它返回一个新对象,对象已排序。它应该与:

一起使用
NSArray *sortedArray = [array_new sortedArrayUsingDescriptors:sortDescriptors];
[self.fetchedObjects addObjectsFromArray:sortedArray];

尽管如此,您不需要单独进行排序。您可以为您的获取请求提供排序描述符,并且执行它时返回的数组将被排序。我强烈建议这样做,而不是你这样做。所以你可以在for循环之前替换所有内容:

    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];

我还在array_new的检查中添加了打印错误。您可能还想在其中添加其他错误处理。