2

I want to be sort NSMutableArray and its structure is something like that,

Object1:NSMutableDictionary

   Affiliation = 2165;
   CallLetters = abc;
   Channel = "2.1";

   ChannelLocation = "";
   ChannelSchedules =         (
   );
   DisplayName = abc;
   DvbTriplets =         (
   );
   FullName = "bc";
   IconAvailable = 1;
   Order = 1;
   ParentNetworkId = 2;
   ServiceType = Digital;
   SourceAttributeTypes =        
       HD

   SourceId = 11222;
   SourceType = Broadcast;
   TiVoSupported = 1;
   Type = "24-Hours";

Object2:NSMutableDictionary

   Affiliation = 1209;
   CallLetters = "xyz";
   Chann?el = "4.1";

   ChannelLocation = "";
   ChannelSchedules =         (
   );
   DisplayName = "xyz";
   DvbTriplets =         (
   );
   FullName = "xyz";
   IconAvailable = 1;
   Order = 2;
   ParentNetworkId = 5;
   ServiceType = Digital;
   SourceAttributeTypes = HD

   SourceId = 111
   SourceType = Broadcast;
   TiVoSupported = 1;
   Type = "24-Hours";
   VirtualChannelNumber = "4.1";

... .. . . . ./

The array contains many objects, and those objects contain many dictionaries. I want to be able to arrange the above array in ascending order using the NSMutableDictionary key "Channel" ?

How can I sort the array?

Andy Shephard
  • 1,726
  • 17
  • 26
Shri Samarth
  • 101
  • 1
  • 12

2 Answers2

3

Use NSSortDescriptor it will work

    NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Channel" ascending:YES];
   [yourarrayobject sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
    [aSortDescriptor release];
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0

To sort your array of objects you:

  1. setup NSSortDescriptor - use names of your variables as keys to setup descriptor for sorting plus the selector to be executed on those keys
  2. get the array of descriptors using NSSortDescriptor that you've setup
  3. sort your array based on those descriptors

How to sort NSMutableArray using sortedArrayUsingDescriptors?

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144