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

Posted on 2012-08-26
Last Modified: 2012-09-12
NSArray *oriArray = [NSArray arrayWithObjects:873,382,113,348, nil];
Question by:charmingduck
    1 Comment
    LVL 14

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    When Apple released Swift last year, the aim was to introduce a new programming language for Cocoa and Cocoa Touch that was fast, easy and effective, like the name connotes. Apple succeeded. Swift is designed to couple with Objective-C program…
         Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON ( If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now