开发者

Sorting NSMutableArray?

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;
   ParentNe开发者_运维技巧tworkId = 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?


Use NSSortDescriptor it will work

    NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Channel" ascending:YES];
   [yourarrayobject sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
    [aSortDescriptor release];


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜