【问题标题】:Order an array based on one property [duplicate]基于一个属性对数组进行排序[重复]
【发布时间】:2017-08-22 04:01:43
【问题描述】:

我有一个名为 allItems 的 NSMutableArray,它具有以下 ProductData 对象。

每个对象都有cid, cname, ctypecimage。正如您在下面看到的,json 对象没有按顺序排列。但是,cid 是一个排序指标。

不知道你们是怎么根据cid排序的?

 [
  {
    "cid": "2",
    "cname": "Meats",
    "ctype": "main",
    "cimage": "baked_chicken.jpg"
  },
  {
    "cid": "1",
    "cname": "Dips",
    "ctype": "side",
    "cimage": "stuffed_eggplant.jpg"
  },
  {
    "cid": "4",
    "cname": "Sandwiches",
    "ctype": "sand",
    "cimage": "chickenshawarma.jpg"
  },
  {
    "cid": "3",
    "cname": "Appetizers",
    "ctype": "side",
    "cimage": "rice_lentils.jpg"
  },
  {
    "cid": "5",
    "cname": "Desserts",
    "ctype": "dsrt",
    "cimage": "cake.jpg"
  }]

如果我使用sortDescriptior,它会部分工作。我面临的问题是,排序时,它首先显示cid 1,然后是cid 10,然后是cid 2。

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES];
[allItems sortUsingDescriptors:@[sortDescriptor]];

【问题讨论】:

  • 您可以为此使用 sortDescriptor
  • 请检查我的更新。

标签: ios objective-c


【解决方案1】:

使用带有比较器的排序描述符传递compare:options: 和选项NSNumericSearch

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES comparator:^(id obj1, id obj2) {
     return [obj1 compare:obj2 options:NSNumericSearch];
}];

甚至更简单:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES selector:@selector(localizedStandardCompare:)];

localizedStandardCompare 是一个特殊的比较选择器:

当文件名或其他字符串出现在列表和表格中时,应使用此方法,其中适合进行类似 Finder 的排序。此方法的确切排序行为在不同的语言环境下有所不同,并且可能会在未来的版本中更改。此方法使用当前语言环境。

【讨论】:

  • 这是迄今为止最好的解决方案。字符串已经知道如何排序,如果它们包含数值。
【解决方案2】:

使用这个

NSSortDescriptor *aSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES comparator:^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
sortedArray = [NSMutableArray arrayWithArray:[unsortedArray sortedArrayUsingDescriptors:@[aSortDescriptor]]];

【讨论】:

    【解决方案3】:
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"cId" 
        ascending:YES selector:@selector(caseInsensitiveCompare:)];
    
    arrToBeSort = [arrToBeSort sortedArrayUsingDescriptors:[NSArray 
        descriptor]];
    

    这会起作用

    【讨论】:

      【解决方案4】:
          NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid.intValue" ascending:YES];
      [array sortUsingDescriptors:@[sortDescriptor]];
      

      您的“数组”在打印后已经排序

      数组打印说明:<__nsarraym>( { cid = 1; cimage = "stuffed_eggplant.jpg"; cname = Dips; ctype = side; }, { cid = 2; cimage = "baked_chicken.jpg"; cname = 肉类;ctype = 主菜;},{ cid = 3;cimage = "rice_lentils.jpg";cname = 开胃菜;ctype = 侧面;},{ cid = 4;cimage = "chickenshawarma.jpg";cname = 三明治;ctype = 沙子; }, { cid = 5; cimage = "cake.jpg"; cname = 甜点; ctype = dsrt; } )

      【讨论】:

      • 它部分有效。我面临的问题,当它排序时,它首先显示cid 1,然后是cid10,然后是cid2。
      • 检查并打印数组
      • @hotspring 为什么它不适合你
      • @AbdelahadDarwish 当你有不同的类型数字时它不起作用,比如十,百,它应该是相同的类型。
      • 只需添加这个 sortDescriptorWithKey:@"cid.intValue"
      猜你喜欢
      • 2021-04-24
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多