1

I am new to objective-c, ios. I'm trying to sort in alfabetic order a NSMutableArray called filteredList that contains objects of type NSString.

so if my mutable array contains : [Mary, Bill, John] I would like to have [Bill, Mary, John]

I did the following:

[filteredList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

but I do not see any change. I did read and tried other solutions like compare: instead of localizedCaseInsensitiveCompare but still nothing.

just ME
  • 1,817
  • 6
  • 32
  • 53
  • https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5 or https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html – Shreyansh Shah Oct 29 '13 at 12:20
  • Your code sorts the mutable array *correctly* to [Bill, John, Mary]. Please show a complete self-contained (non-)working example. – Martin R Oct 29 '13 at 12:33

3 Answers3

2

Updated

 NSArray *sortedArray =[unSortedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];//unSortedArray is NSMutableArray
unSortedArray = [[NSMutableArray alloc]initWithArray:sortedArray];
TamilKing
  • 1,643
  • 1
  • 12
  • 23
  • The array I have is a NSMutableArray. Could you change your code based on the requirements, please? – just ME Oct 29 '13 at 12:06
  • unSortedArray is NSMutableArray you just store the sorted value in NSArray (sortedArray). – TamilKing Oct 29 '13 at 12:08
  • for some reasons the tableview displayes the elements in descending mode - instead of A,B,C I have: C,B,A. Do u have any idea why? – just ME Oct 29 '13 at 12:15
  • @justME:how you add values to the array..Show me that – TamilKing Oct 29 '13 at 12:21
  • I m adding elements using [filterList addObject:nsstring]; I just realised that the unSortedArray have the elements in alphabetic order. For some reason the tableView is switching the list upside down. WHY?:) – just ME Oct 29 '13 at 12:24
  • @justME:Now you got object in array is alphabetic order but tableview displays in wrong correct?.. [tableView reloadData]; after sort the array – TamilKing Oct 29 '13 at 12:27
  • @justME: Please show how you display data in UITableview with sort method – TamilKing Oct 29 '13 at 12:28
  • well I am using the methods cellForRowAtIndexPath - [self.filterList objectAtIndex:indexPath.row] - for each cell in the tableview – just ME Oct 29 '13 at 12:36
  • @justME:are you got correct order in Tableview ?. Simply synthesize filterList; instead of synthesize filterList = _filterList;. I m not sure just try like this – TamilKing Oct 29 '13 at 12:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40182/discussion-between-just-me-and-tamilking) – just ME Oct 29 '13 at 12:50
0

I would do something like:

NSArray *sortedArray = [filteredList sortedArrayUsingComparator:^(NSString *str1, NSString *str2) {
    return (NSComparisonResult)[str1 compare:str2];
}];
filteredList = [sortedArray mutableCopy];
Zalykr
  • 1,524
  • 1
  • 10
  • 20
0

Please refer the below code:-

filteredList = (NSMutableArray*)[filteredList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
Harshil
  • 209
  • 3
  • 10