2014-11-14 36 views
1

我創建簡單的類學生與性能NSSortDescriptor正確排序日期和字符串,這是自定義類的屬性,但正確排序整數

@property (strong,nonatomic) NSString* firstname; 
@property (strong,nonatomic) NSString* lastname; 
@property (strong,nonatomic) NSDate* dateOfBirth; 
@property (assign,nonatomic) NSInteger index; 

在那之後,我創造了這個類的任何對象,給了它名字,姓氏,日期和索引,將它們全部添加到self.InitialStudentsArray。並開始按dateOfBirth排序:

NSSortDescriptor* descriptorDate=[[NSSortDescriptor alloc]initWithKey:@"dateOfBirth" ascending:YES]; 
NSArray* descArray=[NSArray arrayWithObjects:descriptorDate, nil]; 
[self.InitialStudentsArray sortUsingDescriptors:descArray]; 

所以,結果很奇怪。排序之前和排序之後的數組具有不同的項目順序,但根本不按日期排序。

before sort (
    "46 Alex Dupel 25 10 1991", 
    "236 Ivan Evstegneev 20 09 1980", 
    "179 Hugo Igonin 03 02 1992", 
    "189 Alexey Boronenko 06 11 1978") 


after sort (
    "236 Ivan Evstegneev 20 09 1980", 
    "179 Hugo Igonin 03 02 1992", 
    "189 Alexey Boronenko 06 11 1978", 
    "46 Alex Dupel 25 10 1991") 

與名字和姓氏(即使使用選擇同樣的事情:

NSSortDescriptor* descriptorLastname=[[NSSortDescriptor alloc]initWithKey:@"lastname" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

這只是財產「指標」,這是NSInteger的工作

我還做了什麼。 ,我創建了一個只有student.dateOfBirth日期的數組,並且使用keyname self對它進行了排序,並且它可以工作!另一個實驗 - 我創建了帶有關鍵字「dateOfBirth」的字典數組,並按鍵名稱@「dateOfBirth」對它進行了排序,並且它也起作用!

我知道其他方法live sortUsingComparator,但爲什麼這個方法不能在日期和字符串中的屬性?


Upd 哦,夥計們,這是我的錯誤。我感謝你的所有答案,他們把我推到了哪裏,在哪裏搜索錯誤。當我發現它時,我感到非常慚愧。絕對愚蠢的錯誤。如果你有興趣,我用這種方式來初始化學生:

- (instancetype)init 
{ 
    self = [super init]; 
    if (self) { 
     self.firstname=[self getFirstname]; 
     self.lastname=[self getLastname]; 
     self.dateOfBirth=[self getDateOfBirth]; 
    } 
    return self; 
} 
-(NSString*) getFirstname {//some code...arrayOfFirstames...name=arc4random()...} 

即,我偶爾會重新定義設置屬性的getter。三個全部。所以,當我每次使用新數值對數組進行排序時。 謝謝。

+0

它們似乎是按降序排序的firstname。如果您希望使用字符串比較來正確排序,則應該更改日期格式。而不是「DD MM YYYY」使用「YYYY MM DD」。 – 2014-11-14 19:03:17

+0

如果使用'[descriptorDate compareObject:self.InitialStudentsArray [0] toObject:self.InitialStudensArray [1]]',它會返回正確的值嗎? – 2014-11-14 19:06:10

+0

@伊恩麥克唐納,我試過了,它可能會返回正確的值,並可能返回不正確。喜歡它不依賴於項目。喜歡它取決於別的.. – NikLanf 2014-11-14 19:38:03

回答

0

你可能有錯誤的另一件事,SortDescriptors做工精細用繩子,這裏一個例子:在控制檯

NSArray *people = @[@{@"name": @"Ivan"},@{@"name": @"Hugo"},@{@"name": @"Alexey"},@{@"name": @"Alex"}]; 
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
NSArray *sorts = @[sort]; 
NSArray *orderedArray = [people sortedArrayUsingDescriptors:sorts]; 
NSLog(@"Show orderedArray: %@",[orderedArray description]); 

退出:

2014-11-14 20:19:45.377 pro[1366:60b] Show orderedArray: (
    { 
    name = Alex; 
}, 
    { 
    name = Alexey; 
}, 
    { 
    name = Hugo; 
}, 
    { 
    name = Ivan; 
} 
) 

我覺得it's好主意來記錄你的陣列sortedArrayUsingDescriptor方法之前和之後。

嗨,再次。從來就再次做同樣的,使用自定義類,這是頭:

#import <Foundation/Foundation.h> 

@interface CustomClass : NSObject 

@property (nonatomic, strong) NSString *name; 

@end 

實施(.M)是空白的,並再次從來就完成:

CustomClass *ivan = [[CustomClass alloc] init]; 
ivan.name = @"Ivan"; 

CustomClass *hugo = [[CustomClass alloc] init]; 
hugo.name = @"Hugo"; 

CustomClass *alexey = [[CustomClass alloc] init]; 
alexey.name = @"Alexey"; 

CustomClass *alex = [[CustomClass alloc] init]; 
alex.name = @"Alex"; 


NSArray *people = @[ivan,hugo,alexey,alex]; 
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
NSArray *sorts = @[sort]; 
NSArray *orderedArray = [people sortedArrayUsingDescriptors:sorts]; 
NSLog(@"Show orderedArray:"); 
for (CustomClass *people in orderedArray) { 
    NSLog(@"%@\n",people.name); 
} 

再次,退出控制檯是:

2014-11-14 22:38:13.297 pro[1406:147113] Show orderedArray: 
2014-11-14 22:38:13.298 pro[1406:147113] Alex 
2014-11-14 22:38:13.298 pro[1406:147113] Alexey 
2014-11-14 22:38:13.298 pro[1406:147113] Hugo 
2014-11-14 22:38:13.298 pro[1406:147113] Ivan 

sortDescriptors和屬性沒有任何問題。最佳

+0

我確認,在你的例子中,它可以很好地處理字符串,我寫過關於它的內容 - 如果它是字典數組 - 沒問題。但是如果字符串或日期是我自己的類的屬性,並且數組由該類的對象組成,所以它不起作用。 – NikLanf 2014-11-14 19:41:36

+0

我確認這個功能也很好。 – 2014-11-14 21:42:51

+0

是的,它的工作!感謝您的代碼。它有很多幫助。我更新帖子並告訴我錯在哪裏) – NikLanf 2014-11-15 00:41:33