简体   繁体   中英

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.

We cant sort using sortedArrayUsingDescriptors: because the string to compare is not the property of the object of the array to sort.

Say,

The array have objects, those objects have two properties, firstName and 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.

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)

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)

Step 2: Sort the lists (of last names) in descending order

Step 3: Sort bucket of firstName in ascending order

Step 4: Recreate the database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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