1

there have already been lots of questions asked for Sorting of an NSMutableArray still am unable to find the exact solution for my problem.Kindly follow up:

I have an arrMasterArray which is something like this:

    //This is an array of dictionaries with each element having dictionary at its index and key-value for the same.
    <__NSArrayM 0x8bbaca0>(
    {
        "Brand_Description" = Accolate;
        "Medication_DateStart" = "5/5/2012 12:00:00 AM";
    },{
        "Brand_Description" =Trisenox;
        "Medication_DateStart" = "5/5/2012 12:00:00 AM";
    }, {
        "Brand_Description" = Ultiva;
        "Medication_DateStart" = "5/5/2012 12:00:00 AM";
    } ,{
        "Brand_Description" = Accretropin;
        "Medication_DateStart" = "5/5/2012 12:00:00 AM";
    } 

I apply sorting on arrMasterArray which returns me an sorted array but with only elements of that particular keyPath:

NSArray *arrNameCompare= [[arrMasterArray valueForKeyPath:@"Brand_Description"] sortedArrayUsingSelector:@selector(compare:)]; 

which outputs something like this:

<__NSArrayI 0x10e0efb0>(
Accolate,
Accretropin,
Trisenox,
Ultiva
)

Now i want arrMasterArray to get sorted according to **arrNameCompare**.

I searched a lot but no luck. Thanks for your patience and sorry for my english. Thanks Again.

Aniket K.
  • 144
  • 8

2 Answers2

2

i thnk you may read this thread. it will help you

How to sort NSMutableArray using sortedArrayUsingDescriptors?

also for more thisone

How to sort an NSMutableArray with custom objects in it?

Community
  • 1
  • 1
Saad
  • 8,501
  • 2
  • 36
  • 49
1

I think what you're looking for is the NSMutableArray method, sortUsingDescriptors:. This method sorts your (mutable) array "in place" rather than creating a new array like sortedArrayUsingSelector:. So, if I understand what you're trying to do, then these lines will sort arrMasterArray according to the values of Brand_Description:

 NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"Brand_Description" ascending:YES];
 [arrMasterArray sortUsingDescriptors:[NSArray arrayWithObject:sorter]];
rdelmar
  • 102,832
  • 11
  • 203
  • 218