We take the sorting of arrays as an example (perhaps the most common use of NSSortDescriptor is in NSFetchedResultsController, but its usage is roughly the same ).
Assume that you want to sort the objects in the userArray, And the array contains multiple User objects (the User inherits from NSManagedObject). The User has an attribute called country.
[Plain]
1. NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @ "country" ascending: YES];
2. [userArray sortUsingDescriptors: [NSArray arrayWithObject: sortDescriptor];
In this way, you can sort each User's country in ascending or descending order. The sortUsingDescriptors parameter is an array, that is, multiple sort parameters can be used.
For example, the User has a relationship called image, and the image has a property called timestamp. to sort the data according to the timestamp of the User's image relationship, you only need to change the country in the above Code to image. timestamp. That is
[Plain]
1. NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @ "image. timestamp" ascending: YES];
2. [userArray sortUsingDescriptors: [NSArray arrayWithObject: sortDescriptor];
From soloterry's column