0

I have a collection View XIB in Xcode and this is binding to an array controller. I need to display the items in the array controller to the collection view in ascending order but it doesn't working. It displays in random order(i guess). Additionally, the items in the array controller have two string variables "title" and "tag".

How can I solve this problem?

Ryan Moon
  • 1
  • 2
  • you wanted to sort the array asc for title or tag ? – Esha Jul 30 '14 at 09:06
  • Yes, especially for the title. I tried it with "sortDescriptor", but I couldn't control the arrayController with the sortDescriptor function. – Ryan Moon Jul 30 '14 at 09:13
  • can you paste your code here for the cellForItemAtIndexPath and array sorting so some one can bette help you . – Esha Jul 30 '14 at 09:25
  • Thanks esha. I've just solved the problem. It was pretty simple. I used setSortDescriptor function. Because I am a starter of objective-c, it was hard for me. I attached my code below. – Ryan Moon Jul 31 '14 at 07:15

1 Answers1

0

This is my solution. It is pretty easy, huh? I should practice a lot more.

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];

NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];

[self.clipArtArrayController setSortDescriptors:sortDescriptors];

Ryan Moon
  • 1
  • 2
  • Since you're new, let me point out that you shouldn't forget to release the `*sorter` you made. Alternatively you may want to use the class method `+sortDescriptorWithKey:ascending:` of `NSSortDescriptor`. This creates and returns an autoreleased NSSortDescriptor, much in the same way your doing with -alloc: -init:. Your array `sortDescriptors` (and ultimately your `clipArtArrayController`) will retain the `sorter` for you. *(you can happily ignore my comment if you're using ARC however)* – Gerald Eersteling Aug 07 '14 at 11:29