[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I return all elements in an array in ascending order in objective c?

Posted on 2012-08-26
1
Medium Priority
?
586 Views
Last Modified: 2012-09-12
NSArray *oriArray = [NSArray arrayWithObjects:873,382,113,348, nil];
0
Comment
Question by:charmingduck
1 Comment
 
LVL 15

Accepted Solution

by:
Hamidreza Vakilian earned 840 total points
ID: 38334764
First you should notice that initializing NSArray with the syntax you provided above is not correct since can only store NSObjects in NSArray and 873,382,113,348 are of type int. Instead you should use this syntax:

    NSArray * oriArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:873],[NSNumber numberWithInt:382],[NSNumber numberWithInt:113],[NSNumber numberWithInt:348], nil];//We have to use NSNumber for each decimal

    NSArray *sortedArray = [oriArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        
        return (NSComparisonResult)NSOrderedSame;
    }];//There is no built-in sort (as far as I know) in Objective-c which can automatically sort a NSArray which contains NSNumbers. So we have to introduce our desired comparator function to NSArray to make the array sorted.

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating an installable image of an iPhone mobile app (an .ipa file) that beta testers could load onto their test devices. The process involved quite a few steps, but both Appcelerator and Diawi.org had decent …
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question