如何对关系数据进行排序?

时间:2021-10-11 16:55:33

Only a little question:

只有一个小问题:

I have an Entity X with an to-many relation to Entity Y

我有一个与实体Y有多对多关系的实体X.

In the viewDidLoad method i fetch all X and sort them by name and ascending and they are displayed in a tableView.

在viewDidLoad方法中,我获取所有X并按名称和升序对它们进行排序,它们显示在tableView中。

If you choose one of the Xs in the tableView, all relational Y´s are shown in a second tableView. The result of this fetch also includes all relational Y´s for every X, so i dont have to do an extra fetch for the Y´s - but that leads to my Problem: The Y´s are in a completly random order (which also changes with every restart of the app) - but i want them sorted by date (<- a attribute in the Y Entity)... how can i do that?

如果您在tableView中选择其中一个X,则所有关系Y都显示在第二个tableView中。这个获取的结果还包括每个X的所有关系Y,所以我不必为Y进行额外的获取 - 但这导致我的问题:Y是完全随机的顺序(其中每次重启应用程序时也会发生变化) - 但我希望它们按日期排序(< - Y实体中的属性)...我该怎么做?

Heres the fetch - code

继承人的代码

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error.
}

[self setCustomersArray:mutableFetchResults];

and the result looks something like this:

结果看起来像这样:

Entity X-1
 attribute-x-1
 attribute-x-2
 relation-attribute
  relation-entity-y-1
   attribute-y-1
   attribute-y-2
  relation-entity-y-1
   attribute-y-1
   attribute-y-2
Entity X-2
 attribute-x-1
 attribute-x-2
 relation-attribute
  relation-entity-y-1
   attribute-y-1
   attribute-y-2

maybe thats an better example:

也许这是一个更好的例子:

Customer
 name: Bert
 age: 10
 customerMeasureRealtions
  Measure
   score: 5
   date: 10.10.2010
  Measure
   score: 9
   date: 20.20.2000
Customer
 name: Steve
 age: 20
 customerMeasureRealtions
  Measure
   score: 7
   date: 07.07.2007

after the fetch the "Customers" are sorted by "name". But if i access the "Measures" through a "Customer" the order of the "Measures" is random, but i want them sorted by "date".

在获取之后,“客户”按“名称”排序。但是,如果我通过“客户”访问“度量”,“度量”的顺序是随机的,但我希望它们按“日期”排序。

the code for accessing the Measures ("customer" is a Customer-Entity-Object or Model), ("measuresForCustomer" is the customerMeasureRealtions):

访问Measures的代码(“customer”是Customer-Entity-Object或Model),(“measuresForCustomer”是customerMeasureRealtions):

- (void)loadMeasureDataForCustomer:(Customer *)customer
{
NSSet *customerSet = customer.measuresForCustomer;
measuresArray = [NSMutableArray arrayWithArray:[customerSet allObjects]];
[measuresTable reloadData];
}

2 个解决方案

#1


0  

I think there are two ways you could do this:

我认为有两种方法可以做到这一点:

  1. Initiate a second query to your db and fetch the "measures" for a given Customer. Have a look at Core data, sorting one-to-many child objects for this.
  2. 向您的数据库发起第二个查询,并获取给定客户的“度量”。查看Core数据,为此排序一对多子对象。
  3. Transfer the NSSet into an NSArray and sort it in place. An NSSet is always unordered. Have a look at How to sort a NSArray alphabetically?. Or Apple's documentation: sortedArrayUsingDescriptors in conjunction with a NSSortDescriptor.
  4. 将NSSet转移到NSArray并对其进行排序。 NSSet总是无序的。看看如何按字母顺序排序NSArray?或Apple的文档:sortedArrayUsingDescriptors与NSSortDescriptor一起使用。

#2


0  

Use one of the sort methods of NSSet.

使用NSSet的排序方法之一。

Try:

尝试:

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

Or, if you get your NSArray from your NSSet, you then have a whole host of sorting methods on NSArray to pick from:

或者,如果从NSSet获得NSArray,那么在NSArray上有一整套排序方法可供选择:

Sorting
– sortedArrayHint
– sortedArrayUsingFunction:context:
– sortedArrayUsingFunction:context:hint:
– sortedArrayUsingDescriptors:
– sortedArrayUsingSelector:
– sortedArrayUsingComparator:
– sortedArrayWithOptions:usingComparator:

These come from NSArray and NSSet API documents.

这些来自NSArray和NSSet API文档。

#1


0  

I think there are two ways you could do this:

我认为有两种方法可以做到这一点:

  1. Initiate a second query to your db and fetch the "measures" for a given Customer. Have a look at Core data, sorting one-to-many child objects for this.
  2. 向您的数据库发起第二个查询,并获取给定客户的“度量”。查看Core数据,为此排序一对多子对象。
  3. Transfer the NSSet into an NSArray and sort it in place. An NSSet is always unordered. Have a look at How to sort a NSArray alphabetically?. Or Apple's documentation: sortedArrayUsingDescriptors in conjunction with a NSSortDescriptor.
  4. 将NSSet转移到NSArray并对其进行排序。 NSSet总是无序的。看看如何按字母顺序排序NSArray?或Apple的文档:sortedArrayUsingDescriptors与NSSortDescriptor一起使用。

#2


0  

Use one of the sort methods of NSSet.

使用NSSet的排序方法之一。

Try:

尝试:

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

Or, if you get your NSArray from your NSSet, you then have a whole host of sorting methods on NSArray to pick from:

或者,如果从NSSet获得NSArray,那么在NSArray上有一整套排序方法可供选择:

Sorting
– sortedArrayHint
– sortedArrayUsingFunction:context:
– sortedArrayUsingFunction:context:hint:
– sortedArrayUsingDescriptors:
– sortedArrayUsingSelector:
– sortedArrayUsingComparator:
– sortedArrayWithOptions:usingComparator:

These come from NSArray and NSSet API documents.

这些来自NSArray和NSSet API文档。