2011-04-30 83 views
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]); 
} 

回答

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時添加了打印錯誤。您可能還想在其中添加其他錯誤處理。

+0

hi mccygnus.thanks for your response it is working ......... – girish 2011-04-30 11:07:25