简体   繁体   English

如何按2列/键对对象的NSArray进行排序

[英]How to sort an NSArray of objects by 2 column/key

I want to sort an array using one or more conditions of objects based in its property. 我想根据其属性使用一个或多个对象条件对数组进行排序。

Each object in the array contain an object which in turn has an array 2 or more NSString property. 数组中的每个对象都包含一个对象,该对象又具有数组2或更多的NSString属性。

We cant sort using sortedArrayUsingDescriptors: because the string to compare is not the property of the object of the array to sort. 我们无法使用sortedArrayUsingDescriptors进行排序:因为要比较的字符串不是要排序的数组对象的属性。

Say, 说,

The array have objects, those objects have two properties, firstName and lastName. 数组具有对象,这些对象具有两个属性,firstName和lastName。

I want to sort the array by two conditions, 我想按两个条件对数组进行排序,

1- sort ascending/descending the array by firstName 2- then sort ascending/descending the array again by lastName when firstName is same. 1-按firstName对数组进行升/降排序2-然后当firstName相同时,按lastName对数组进行升/降排序。

So far I tried Insertion sort to sort by one object. 到目前为止,我尝试了将插入排序按一个对象排序。 Works well if I wish to sort by one condition. 如果我希望按一种条件排序,效果很好。

After looking I have found this question Xcode sort array by specific (NSString) dictionary key (like sql query) 看完后,我发现此问题按特定(NSString)字典键(例如sql查询)的Xcode排序数组

But its not answered. 但它没有回答。

Help will be appreciated. 帮助将不胜感激。

使用sortedArrayUsingDescriptors:或可变数组副本)对数组进行排序,并为数组提供2个排序描述符(第一个用于名字,第二个用于姓氏)。

I pondered for a while and got an answer 我琢磨了一会儿,得到了答案

this custom written method 这种自定义的书面方法

[self getStringforObject:obj2 forConditionInKey:condition.key];

Will return the string I want to compare in the object. 将返回我要在对象中比较的字符串。

 NSArray *  allSortedObjects= [allobjects sortedArrayUsingComparator:^NSComparisonResult(id  obj1 , id obj2)
                          {
                            for (MYCondition *condition in [conditionArray allobjects] )
                              {

                                    if (condition.orderType==NSOrderedSame)
                                  {


                                      continue; // breaks when no sorting is needed
                                  }
                                  NSString * string1=[self getStringforObject:obj1 forConditionInKey:condition.key];
                                  NSString * string1=[self getStringforObject:obj2 forConditionInKey:condition.key];


                                  NSComparisonResult result=[string1 compare:string2 options:NSCaseInsensitiveSearch | NSNumericSearch];
                                  if (result == NSOrderedSame)
                                  {
                                      continue;
                                  }
                                else if(condition.orderType==NSOrderedDescending)
                                  {//when sorting needed is descending. 
                                      if (rexult==NSOrderedAscending)
                                      {
                                          return NSOrderedDescending;
                                      }
                                      else
                                      {
                                          return NSOrderedAscending;
                                      }

                                  }

                                  return result;
                              }

                              return NSOrderedSame;
                          }];

if you want to create your own algorithm then this high level view of algorithm can help you:- 如果您想创建自己的算法,那么这种高级算法视图可以帮助您:-
Step 1: Create a bucket with firstName as value and lastnames as List for particular value (means lastNames with same fistName will be in the same list) 步骤1:创建一个存储桶,将firstName作为值,并将lastnames作为List的特定值(意味着具有相同fistName的lastName将在同一列表中)

Step 2: Sort the lists (of last names) in descending order 步骤2:按降序对(姓氏)列表进行排序

Step 3: Sort bucket of firstName in ascending order 步骤3:按升序对firstName的存储桶进行排序

Step 4: Recreate the database 步骤4:重新创建数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM