核心数据(SQLite支持)自定义字符串排序 [英] Core Data (SQLite backed) custom sorting of strings

查看:397
本文介绍了核心数据(SQLite支持)自定义字符串排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个SQLite支持的核心数据应用程序,其中我获取记录排序firstName然后lastName。



问题是我有一些名字,例如(John Doe),他们回到了顶部列表,似乎默认的排序行为是以ASCII排序顺序完成的。



是否仍然强制这些记录显示在 Z



我已经阅读了关于自定义排序描述符,但是它们似乎不工作,因为Core Data使用SQLite进行排序。



/ strong>



以下是我的数据现在的样例列表

 (Abe Simpson)
(Bob Dole)
(Chris Rock)
Alan West
Brian Regan

我想让它看起来像

  Alan West 
Brian Regan
(Abe Simpson)
(Bob Dole)
(Chris Rock)


解决方案

添加一个新的BOOL属性括号,然后...

   - (void)setName:(NSString *)name 
{
[self willChangeValueForKey:@name];
[self setPrimitiveName:name];
[self didChangeValueForKey:@name];
self.parenthesized =([name hasPrefix:@(]&&
[name hasSuffix:@)]);
}

当你执行fetch时:



[fetchRequest setSortDescriptors:
[NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@括括括号升序:NO],
[NSSortDescriptor sortDescriptorWithKey:@nameascending:YES],
nil]];


I currently have a SQLite backed Core Data app where I am fetching records sorted on firstName and then lastName.

The issue is that I have some names like (John Doe) and they are coming back at the top of the list, it seems the default sorting behavior is done in ASCII sort order.

Is there anyway to force these records to show up after Z?

I have read about custom sort descriptors but they do not seem to work because Core Data falls back on SQLite for sorting.

Update

Here is a sample list of what my data looks like now

(Abe Simpson)
(Bob Dole)
(Chris Rock)
Alan West
Brian Regan

What I want it to look like

Alan West
Brian Regan
(Abe Simpson)
(Bob Dole)
(Chris Rock)

解决方案

Add a new BOOL attribute called parenthesized, then...

- (void)setName:(NSString *)name
{
    [self willChangeValueForKey:@"name"];
    [self setPrimitiveName:name];
    [self didChangeValueForKey:@"name"];
    self.parenthesized = ([name hasPrefix:@"("] &&
                          [name hasSuffix:@")"]);
}

And when you do the fetch:

[fetchRequest setSortDescriptors:
 [NSArray arrayWithObjects:
  [NSSortDescriptor sortDescriptorWithKey:@"parenthesized" ascending:NO],
  [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES],
  nil]];

这篇关于核心数据(SQLite支持)自定义字符串排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆