• 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: Michael Crawford <email@hidden>
  • Date: Fri, 11 Feb 2011 10:19:40 -0500

Thank you both for your suggestions.  I ended up going with Ken's idea regarding the synthetic property.  It was added to a NSManagedObject derived class so I couldn't use it during the fetch request without actually adding an attribute but I did use it to sort the resulting array.  The synthetic property computes an priority index when invoked.  It performs very well.

-Michael

On Feb 8, 2011, at 5:19 PM, Ernesto Giannotta wrote:

>
> 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: Ken Thomases <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>)
 >Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data) (From: Ernesto Giannotta <email@hidden>)

  • Prev by Date: Re: Strange problem drawing a custom grid in NSTableView
  • Next by Date: Re: Need help sorting with NSSortDescriptor and/or/NSPredicate (Core Data)
  • 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