Have a NSMutable Array that add subarrays with key, i try sort by "distkm", but not sort:
[alldata addObject:#{#"sublista": sublista, #"subdetail": subdetail, #"distkm": distkm }];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"distkm" ascending:YES];
alldata = (NSMutableArray *)[alldata sortedArrayUsingDescriptors:#[descriptor]];
But result is same before sort:
Result of sort array
Agree some help.
THANKS
Related
I cannot get sorting working on a view based tableview for a CoreData App using multiple sort descriptors.
I have 2 columns, my first column has "bank" and "accountNickName" as values , my second column has "bankAccount".
I want to sort by "bank" then "accountNickName" and then by "bankAccount". If I click on the 1st column.
I want to sort by "bankAccount", then "bank", then "accountNickName".
If I click on the second column.
Creating an array of sortDescriptors and bind this to my arraycontroller doesn't work:
sortForStockInfo = [ NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:#"bank" ascending:YES selector:#selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:#"accountNickName" ascending:YES selector:#selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:#"account" ascending:YES selector:#selector(compare:)],
nil];
The tableview has "Sort Descriptors" bound to the Array Controller's "Sort Descriptors". I thought, this is all I had to do. But it doesn't work. What have I missed out ?
Strange enough: If I use the same approach, but I fill the columns attributes for Sortkey, Selector and Order, it is sorting only for one aspect (e.g. bank or account. accountNickName remains unsorted ) . Because I can only define one criteria per column.
sortDescriptors is an array, the sortDescriptor of the clicked column is inserted at index 0. When the user clicks on column 0 and then on column1, the sort order is column1, column0.
Implement delegate method - (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn and set sortDescriptors of the arraycontroller. Example:
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn {
NSArray *sortDescriptors = self.arrayController.sortDescriptors;
if (sortDescriptors && [sortDescriptors count] > 0) {
NSSortDescriptor *firstSortDescriptor = sortDescriptors[0]; // sort descriptor of the clicked column
BOOL ascending = firstSortDescriptor.ascending;
if ([firstSortDescriptor.key isEqualToString:#"bank"]) {
self.arrayController.sortDescriptors = #[
[NSSortDescriptor sortDescriptorWithKey:#"bank" ascending:ascending selector:#selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:#"accountNickName" ascending:ascending selector:#selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:#"account" ascending:ascending selector:#selector(compare:)]];
}
else
if ([firstSortDescriptor.key isEqualToString:#"account"]) {
self.arrayController.sortDescriptors = #[
[NSSortDescriptor sortDescriptorWithKey:#"account" ascending:ascending selector:#selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:#"bank" ascending:ascending selector:#selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:#"accountNickName" ascending:ascending selector:#selector(compare:)]];
}
}
}
I have a NSMutableArray with data:
Name ... Male ... Age ... Point
("David"..."Male"...18...7.25)
("Mary"..."Female"...16...20.50)
("Kend"..."Male"...17...4.50)
("Frank"..."Male"...25...27.50)
("Cain"..."Female"...28...2.25)
("Sims"..."Female"...17...10.50)
...
I want to sort this NSMutableArray by key Point. I tried:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Point" ascending:NO];
[self.arrayPupils sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
and output:
David/Male/18/7.25
Kend/Male/17/4.50
Frank/Male/25/27.50
Mary/Female/16/20.50
Cain/Female/28/2.25
Sims/Female/17/10.50
But expected result are:
Frank/Male/25/27.50
Mary/Female/16/20.50
Sims/Female/17/10.50
David/Male/18/7.25
Kend/Male/17/4.50
Cain/Female/28/2.25
Help me pls... Many thanks.
Your sort is sorting by Age. You need to instead sort by "Point" to get that output.
I am working on NSMutable Array containing list of "First Name" in it.I want to sort it alphabatically.
I tried this code
NSSortDescriptor * sortFriend = [[[NSSortDescriptor alloc] initWithKey:kfirstName ascending:YES] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortFriend];
NSArray * sorted = [Friendsarr sortedArrayUsingDescriptors:descriptors];
correct me if i am wrong.I do have an array of names also if that could be used.
Thanks
I tried the code
NSSortDescriptor * sortFriend = [[[NSSortDescriptor alloc] initWithKey:kfirstName ascending:YES selector: #selector(caseInsensitiveCompare:)] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortFriend];
NSArray * arrmp = [temparray sortedArrayUsingDescriptors:descriptors];
But still my result contain 2 'k' chars. one 'k' and 'K'.I want to have case insensitive result
If your NSMutableArray only contains objects of type NSString, simply do:
[myMutableArray sortUsingSelector:#selector(compare:)];
You can sort NSMutable array case insensitively by this code
NSSortDescriptor *sorter=[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:#selector(caseInsensitiveCompare:)]autorelease];
NSArray *sortdescriptor=[NSArray arrayWithObject:sorter];
[cpy_arr_Service_Name sortUsingDescriptors:sortdescriptor];
I found the solution of sorting NSMutableArrays with sample code which can be found on the site below.
http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
I hope it help others to as it provides indexed sorting of a grouped table view using an array as the datasource
Is there a way to sort on finish times for a race? I have an NSArray with a dictionary of NSStrings and one of the values is race finish time as an NSString. It sorts them all correctly as long as they have the same number of integers, like this: 54.13, 54.32, 54.52... but when one of them is longer, for example: 1:13.42 -- It puts it at the top of the list. How should I sort this so the slower times are at the bottom of the list. Here is what I currently have:
NSSortDescriptor * sortByTime2 = [[NSSortDescriptor alloc] initWithKey:#"raceTime" ascending:YES];
NSArray * descriptors2 = [NSArray arrayWithObject:sortByTime2];
myArray = [myArray sortedArrayUsingDescriptors:descriptors2];
thanks,
R
The strings "raceTime" are just mere string, they don't represent time. When you want to compare these, you need to convert these string to instances of NSData, then sort. You can use NSSortDescriptor and initialize it with a selector which will do the conversion and return the appropriate order.
I have a NSMutableArray with NSStrings and I want to sort them in alphabetical order, seems it's no hard, but I didn't find nice solution. Help me please.
Using sortUsingSelector: method with compare: as comparing selector:
NSMutableArray* strings = [NSMutableArray arrayWithObjects:#"dog", #"ant", #"cat",nil];
[strings sortUsingSelector:#selector(compare:)];
NSLog(#"%#", strings);