简体   繁体   English

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

[英]problem with NSSortDescriptor .i am sorting the in ascending order(alphabetical order)

I am using NSSortDescriptor to sort the custom objects with key bandName , but I am not getting the output in alphabetical order.我正在使用NSSortDescriptor使用键bandName对自定义对象进行排序,但我没有按字母顺序获取 output。 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]);  
}

sortedArrayUsingDescriptors: doesn't sort in place the array it was sent to. sortedArrayUsingDescriptors:不对它发送到的数组进行排序。 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.您可以将排序描述符提供给您的 fetch 请求,并且从执行它返回的数组将被排序。 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:因此,您可以将 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];

I also added in the check for array_new to print the error.我还添加了对 array_new 的检查以打印错误。 You might want to add additional error handling there too.您可能还想在那里添加额外的错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM