【问题标题】:Sorting NSMutableArray?排序 NSMutableArray?
【发布时间】:2011-12-06 20:14:39
【问题描述】:

我想对 NSMutableArray 进行排序,它的结构是这样的,

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";

... .. . . . ./

数组包含许多对象,这些对象包含许多字典。我希望能够使用 NSMutableDictionary 键“Channel”按升序排列上述数组?

如何对数组进行排序?

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    使用 NSSortDescriptor 就可以了

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

    【讨论】:

      【解决方案2】:

      要对对象数组进行排序:

      1. 设置 NSSortDescriptor - 使用变量名作为键 用于排序的设置描述符以及要在其上执行的选择器 那些键
      2. 使用 NSSortDescriptor 获取描述符数组 设置
      3. 根据这些描述符对数组进行排序

      How to sort NSMutableArray using sortedArrayUsingDescriptors?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        相关资源
        最近更新 更多