1

I have an array like:

( "8461.86","8468.22","8468.22","8162.59","8164.50","7280.59" )

I try to sort this array decendingly as per following:

sortedArray = [arr_distance sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];

But i got following array like:

( "7280.59","8162.59","8164.50","8461.86","8468.22","8468.22" )

Why this happen?

I also try with like

return [firstObject compare:secondObject options:NSNumericSearch];

But not getting proper sorted array.

2
  • 1
    Where's the question about Xcode? Commented Apr 16, 2013 at 6:25
  • 1
    The sort you provided is sorting in ascending order. This is the default. Commented Apr 16, 2013 at 6:31

4 Answers 4

2
NSSortDescriptor *desc = [[NSSortDescriptor alloc]initWithKey:@"" ascending:NO];
NSMutableArray *yourTempArray = [[NSMutableArray alloc]initWithObjects:@"8459.93",@"8466.28",@"8466.28",@"8160.70",@"8162.61",@"7278.56", nil];
[yourTempArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
NSLog(@"sotedAr == %@",yourTempArray);

Output:

sotedAr == (
"8466.28",
"8466.28",
"8459.93",
"8162.61",
"8160.70",
"7278.56"

)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply! But my values can be repeated i.e. same value can repeated more than once.
So what ? It gives you in the sorted list rite? For Ex: 5,6,7,8,5 then output comes as 8,7,6,5,5 rite ?
thanks for your reply! But when i give array like: NSMutableArray *yourTempArray = [[NSMutableArray alloc]initWithObjects:@"8459.93",@"8466.28",@"8466.28",@"8160.70",@"8162.61",@"7278.56", nil]; It is not properly sorted
dude don't copy and paste your objects. See the output and i got the output as above
sorry i made some mistake that's why i didn't get proper array
2

To sort in descending order, you could just switch the firstObject and secondObject in your code:

sortedArray = [arr_distance sortedArrayUsingComparator:^(id firstObject, id secondObject) {
  return [((NSString *)secondObject) compare:((NSString *)firstObject) options:NSNumericSearch];
}];

Comments

1

Try following code:-

NSSortDescriptor *frequencyDescriptor = [NSSortDescriptor sortDescriptorWithKey:@""
                                                               ascending:NO
                                                              comparator:^(id obj1, id obj2) {
                                                                  return [obj1 compare:obj2 options:NSNumericSearch];
                                                              }];
NSEnumerator * enumerator = [arr_distance objectEnumerator];
NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil];
NSArray * sortedArray =   [arr_distance sortedArrayUsingDescriptors:descriptors];
enumerator = [sortedArray objectEnumerator];
NSLog(@"sortedArray = %@",sortedArray);

// use sortedArray which is sorted

2 Comments

Thanks for Your Reply! But not get properly sorted array. I have distance array like:("8459.93","8466.28","8466.28","8160.70","8162.61","7‌​278.56")
sorry, you are right. Due my mistake i didn't get proper array. I like to give up vote but due to low Reputation, I can't do it. Sorry once again...
1

Below code may be useful for sorting your array in descending order

NSArray *arr = [NSArray arrayWithObjects:@"8459.93",@"8466.28",@"8466.28",@"8160.70",@"8162.61",@"7278.56", nil];
NSMutableArray *yourTempArray = [[NSMutableArray alloc]init];
[yourTempArray addObjectsFromArray:arr];
NSLog(@"before sorting = %@",yourTempArray);
NSSortDescriptor* sort = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];

[yourTempArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"After sorting = %@",yourTempArray);

4 Comments

Thanks for your reply! But i am not getting the sorted array.
@user2243212,Did u tried that code, it will surely sort the arrray in descending order
@user2243212, just copy and paste the above code in your project's viewdidload function , run the project and see the output
sorry, you are right. Due my mistake i didn't get proper array. I like to give up vote but due to low Reputation, I can't do it. Sorry once again..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.