Ios 使用具有多个密钥和丢失数据的NSSortDescriptor

Ios 使用具有多个密钥和丢失数据的NSSortDescriptor,ios,nssortdescriptor,Ios,Nssortdescriptor,我想对用户创建的联系人进行排序,其中的联系人具有大杂烩的数据。有些联系人可能填写了每个字段或属性,例如姓、名、公司、电话、电子邮件,而其他联系人可能只填写了名、电子邮件或公司 使用NSSort描述符的惯用方法是创建一个NSArray,其中数组中的第一个SortDescriptor优先,每个连续的SortDescriptor都会断开连接。因此: NSSortDescriptor *last =[[NSSortDescriptor alloc]initWithKey:@"lastname" asce

我想对用户创建的联系人进行排序,其中的联系人具有大杂烩的数据。有些联系人可能填写了每个字段或属性,例如姓、名、公司、电话、电子邮件,而其他联系人可能只填写了名、电子邮件或公司

使用NSSort描述符的惯用方法是创建一个NSArray,其中数组中的第一个SortDescriptor优先,每个连续的SortDescriptor都会断开连接。因此:

NSSortDescriptor *last =[[NSSortDescriptor alloc]initWithKey:@"lastname" ascending:YES];
NSSortDescriptor *first =[[NSSortDescriptor alloc]initWithKey:@"firstname" ascending:YES]; 
NSSortDescriptor *company =[[NSSortDescriptor alloc]initWithKey:@"company" ascending:YES]; 
NSSortDescriptor *email =[[NSSortDescriptor alloc]initWithKey:@"email" ascending:YES];   
[request setSortDescriptors:[NSArray arrayWithObjects:last, first, company,email, nil]];
适用于

  Allen,Joe,microsoft,ja@microsoft.com|Allen,Zack,google,nil|Smith,Tom,Apple,nil 
将给你:

Allen, Joe
Allen, Zack
Smith, Tom
cloudflare
raj@gmail.com
Zack
Allen, Joe
看起来不错

但是,如果您的数据如下所示:

Allen,Joe,nil,ja@gmail.com|nil,Zack,nil,zack@msn.com|nil,nil,nil,raj@gmailcom|nil,nil,cloudflare,nil
以上内容将为您提供:

Allen, Joe
Allen, Zack
Smith, Tom
cloudflare
raj@gmail.com
Zack
Allen, Joe
真是一团糟

在第二种情况下,我想做一些更有意义的事情,如果只对电子邮件进行排序,然后再对公司进行排序,然后是姓氏,然后是名字

raj@gmail.com
cloudflare
Allen, Joe
Zack
或者,甚至是他们按顺序排列的任何领域

Allen, Joe
cloudflare
raj@gmail.com
Zack
有没有办法做到这一点?苹果是如何使用他们的联系人的


感谢您提供任何建议

而不是使用

initWithKey:ascending:
你可以用

initWithKey:ascending:comparator:
在比较器块中

^NSComparisonResult(id obj1, id obj2)  {…}
您可以显式检查
nil
对象,并根据需要设置比较结果。
我没有检查这个,但它应该可以工作