简体   繁体   中英

Sorting Array Alphabetically for section tableview

How to sort an array alphabetically to return all objects data instead of single name? And how to set these data in section tableview? Number of rows in section method - how it works, please help me.

You can sort your array with a sort descriptor. in the sort descriptor you set the attrib of the array elements which shall be used for sorting:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"yourAttribInArrayElememts" ascending:YES];
NSArray *sortedArray = [yourArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

Create this method inside your Object class

-(NSComparisonResult) compareWithObjectClass: (YourObjectClass *) otherObject
{
  return [self.name compare:otherObject.name]
}

Then, in your viewcontroller, you can call sortUsingComparator method to order your array of objects

 //Order items Array
[self.itemsArray sortUsingComparator:^NSComparisonResult(YourObjectClass *obj1, YourObjectClass *obj2){
           return [obj1compareWithObjectClass:obj2];
      }];

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