• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)


  • Subject: Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)
  • From: Ernesto Giannotta <email@hidden>
  • Date: Tue, 8 Feb 2011 23:19:57 +0100

On 07-feb-2011, at 16:29, Ken Thomases wrote:

> On Feb 7, 2011, at 8:56 AM, Michael Crawford wrote:
>
>> I'm trying to implement a feature where I sort a cross-section of music from multiple genres with up to two different keys, where the items with a genre matching the first key appear first in the collection and items with a genre matching the second key appear after the first group in the collection.  All songs with a genre not matching the first or second keys can be in any order following the first two groups.
>>
>> So I took a look at NSSortDescriptor but it is not apparent to me how I would accomplish my goal with this class.  If I sort with it based on the genre key, will basically sort them in alphabetical ascending or descending order, not what I want.
>
> Here are two possible approaches:
>
> 1) Implement a synthetic property which is more amenable to the sort ordering you want.  Something which returns 0 for the first genre, 1 for the second, and 2 for all others, for example.
>
> 2) Implement a custom comparator method that compares the existing genre property in the manner you want: the first genre equal to itself but coming before all others; the second genre coming after the first, equal to itself, and coming before the remaining genres; all other genres coming after the first two but unordered with respect to each other.
>
> Either or both of these methods could be implemented in a category if that makes things easier.
>

Or subclass the NSSortdescriptor and implement

- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2


Cool Runnings,
Erne.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)
      • From: Michael Crawford <email@hidden>
References: 
 >Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data) (From: Michael Crawford <email@hidden>)
 >Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data) (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: CoreData Migration Problems
  • Next by Date: Initializing NSError **, again Re: CoreData Migration Problems
  • Previous by thread: Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)
  • Next by thread: Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)
  • Index(es):
    • Date
    • Thread