Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Sorting NSArray with custom objects based on its attribute order in a base array

raheelraheel Posts: 109Registered Users
whaa,, thats a long thread name,

I have a custom NSManagedObject called 'Field' and it has an pkey attribute which is an NSString.

Now i fetch those objects from a core data relationship, and i get an NSSet.

So i have this array:

FieldsArray { 
Field title=ActionJackson, id=jack
Field title=Bond, James, id=007
Field title=Bourne, Jason id=treadstone
}


This mite sound lame, but i store the required order of this array in another Array, SO i already have an array of NSStrings
SortUsingArray = {
treadstone,
jack,
007
}



So i want to sort FieldsArray (which is a managedObject) based on the ORDER of SortUsingArray (an array of strings)

And the desired result should be:
FieldsArray {
Field title=Bourne, Jason id=treadstone
Field title=ActionJackson, id=jack
Field title=Bond, James, id=007
}



Been breaking my head over this for a while using all sorts of descriptors and arrays, but i couldn't get past the object.attribute sorting based on sample attribute ordered Array. Don't even kno what i said there..
Post edited by raheel on

Replies

  • FstuffFstuff Posts: 154Registered Users
    Frankly I think the easiest thing to do would be to either place your results into a dictionary or to just iterate through your Sort Array to manually sort your Results array.

    Usually you would use an NSSortDescriptor. You can specify a custom selector for an NSSortDescriptor. The idea behind NSSortDescriptor is that for any two objects in your array, you can 'compare' them such that A is greater than, less than, or equal to, B. So, you will need to write a custom selector for use with your array.

    Of course, you need to understand how custom sort comparisons work. From the docs:
    The selector used for the comparison is passed a single parameter, the object to compare against self, and must return the appropriate NSComparisonResult.
    Which means that somehow each of your managed objects must know which array contains the arbitrary sorting method you are selecting. I don't know why you need this custom sort order, or whether it's something you can abstract out, but it seems a little unwieldy. Maybe there is a better way of achieving what you want than by having a custom sort array? Would your objects be sorted differently at different times? If not, you could just have a 'sortOrder' property on your managed objects. Would make things a lot easier for you.
  • raheelraheel Posts: 109Registered Users
    Fstuff;427840 said:
    Frankly I think the easiest thing to do would be to either place your results into a dictionary or to just iterate through your Sort Array to manually sort your Results array.

    Usually you would use an NSSortDescriptor. You can specify a custom selector for an NSSortDescriptor. The idea behind NSSortDescriptor is that for any two objects in your array, you can 'compare' them such that A is greater than, less than, or equal to, B. So, you will need to write a custom selector for use with your array.

    Of course, you need to understand how custom sort comparisons work. From the docs:

    Which means that somehow each of your managed objects must know which array contains the arbitrary sorting method you are selecting. I don't know why you need this custom sort order, or whether it's something you can abstract out, but it seems a little unwieldy. Maybe there is a better way of achieving what you want than by having a custom sort array? Would your objects be sorted differently at different times? If not, you could just have a 'sortOrder' property on your managed objects. Would make things a lot easier for you.
    Thanks for the answer man,

    Yea it is really weird.

    The array i get (to be sorted) is actually an NSSet from an entity relationship. I could add a displayOrder or something, the entity is in a relationship with itself, Believe me, this is the last thing i wanted. But i couldn't justify a head "category". Even so, the category would have a many-to-many relationship with the child entity. in which case a child having a sortOrder attribute was useless.


    that was the problem, so i stored the order as delimited strings (which i them separate and create an array from) child entity in an attribute., (I shouldn't call it child as theres no category ,parent entity )..



    IM puzzled with the comparisons, and couldn't get over it, help in code would be great, I'm sure i'll grasp the concept that way..

    thanks :) ,
Sign In or Register to comment.