Reputation: 1758
i sort my array by experience using this code
sortexp = [NSSortDescriptor sortDescriptorWithKey: @"Experience" ascending:YES];
arrData = [arrData sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortexp]];
NSLog(@"%@",arrData);
[tblResponseData reloadData];
NSLog(@"Experience");
but problem is that it's wrong count when compare single digits and double digits number.
example - array = {10,3,11,35,6,7}
and after sort it will array = {10,11,3,35,6,7}
so how can i convert my single digit number in double digits using prefix 0
with number , my data is come from server so i can't change it so i do it's my side.
Upvotes: 0
Views: 755
Reputation: 4909
Basically this is happening because you are sorting your numbers as string so it gives result in such a way (3 > 11 means it compares only first digit and if first digit same then compare next one) , you do not need to change the digits but sort them according to numbers not as string. For this you should use
sortexp = [NSSortDescriptor sortDescriptorWithKey: @"Experience" ascending:YES selector:@selector(localizedStandardCompare:)];
It will compare your numbers as number and string as string.
EDIT:
You can sort Using blocks as well.
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([[obj1 valueForKey:@"Experience"] integerValue] > [[obj2 valueForKey:@"Experience"] integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[obj1 valueForKey:@"Experience"] integerValue] < [[obj2 valueForKey:@"Experience"] integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
Upvotes: 7