0

I have sorted some NSMutableArray, that has dictionaries with many fields . 2 fields are interested me the most , the date and some count integer number .

I have sorted the array with date :

 NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"dateCreated"  ascending:YES];
   sortedWithDates=[sortedWithDates sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

Now, i want to take only the objects in the array -for today, and sort them again according to this count integer(highest first) .

Is there a simple way to do that with the same NSSortDescriptor class ?

Thank you .

Curnelious
  • 1
  • 10
  • 65
  • 133
  • possible duplicate of [How to sort an NSMutableArray with custom objects in it?](http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it) – Antzi Apr 14 '14 at 16:57
  • It is definitely not a duplicated . read carefully the question , and the link you gave me. i already knows how to sort array. i want to sort only part of it with the sortDescriptor. – Curnelious Apr 14 '14 at 16:58

2 Answers2

3

Is there a simple way to do that with the same NSSortDescriptor class ?

Yes, there's a very simple way. You can pass more than one sort descriptor to -sortedArrayUsingDescriptors:. Items that are equal according to the first descriptor will be sorted according to the second (or third, etc.).

From the docs:

The first descriptor specifies the primary key path to be used in sorting the receiving array’s contents. Any subsequent descriptors are used to further refine sorting of objects with duplicate values.

So your code could look like this:

NSSortDescriptor *date = [[NSSortDescriptor alloc] initWithKey:@"dateCreated"  ascending:YES];
NSSortDescriptor *count = [[NSSortDescriptor alloc] initWithKey:@"count"  ascending:YES];
sortedWithDates=[sortedWithDates sortedArrayUsingDescriptors:@[date, count]];

Note: I used the newer object literal syntax for the array of descriptors. That's just a matter of style -- your +arrayWithObjects: call is fine too.

Caleb
  • 120,112
  • 19
  • 171
  • 259
  • but something is missing to me with this. because if the array sorted once with dates , and the first object is the newest date , than i sort it again with another field, and the oldest object has a bigger number than the newer, it will replace the newer. i want to give more power to date , than to this count number. – Curnelious Apr 14 '14 at 17:01
  • Thanks, i see the edit from the docs, still ,there is a problem with this, because all the dates of TODAY ,will be never the same, because NSDate is including the time till the seconds. so if i will put another sorting line, the dates of today will remain the same-because they are not equals.. – Curnelious Apr 14 '14 at 17:07
  • @Curnelious I've added a quote from the documentation that may clarify things for you. In the example I gave, the items will be sorted first according to date. If two or more objects have the same date, so that they could appear in any order in the sorted list, the second sort descriptor is used to decide the order. If multiple objects are equal according to both the first and second sort descriptors, then the third will be used, and so on. – Caleb Apr 14 '14 at 17:07
  • see my comment above. – Curnelious Apr 14 '14 at 17:09
  • @Curnelious It's true that you might have several date objects that all mean "today" but have different times. It sounds like you want to consider only the year, month, and day, but not smaller date components like hours and minutes. One way to deal with that is to add another key like "dayCreated" that returns a more limited date, which you can create using e.g. NSDateComponents. You'll find lots of questions and answers about working with dates here on SO. – Caleb Apr 14 '14 at 17:11
0

You can use the same descriptor as often as you like. Of course, the descriptor that you created will always sort on "dateCreated" in ascending order, but it can be used to sort any number of arrays.

Instead of [NSArray arrayWithObjects:descriptor,nil] you can use the modern syntax @[descriptor]. @[object ... ] creates an NSArray containing the objects between the square brackets.

gnasher729
  • 47,695
  • 5
  • 65
  • 91