Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:46:47+00:00 2026-06-03T13:46:47+00:00

I got problem for sorting array of nsdate. so i have array of date

  • 0

I got problem for sorting array of nsdate.
so i have array of date inside nsmutablearray

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSArray *arrString = [[NSArray alloc] initWithObjects:@"2012-05-01", @"2012-02-01", @"2012-22-03", @"2012-05-01", @"2012-15-01", nil];
NSMutableArray *arrDate = [[NSMutableArray alloc] initWithCapacity:arrString.count];
for (NSString *item in arrString) {
    NSDate *date = [dateFormatter dateFromString:item];
    [arrDate addObject:date];
}

as i saw in many cases they use code like this to sort array:

NSArray *sortedArray = [arrDate sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSDate *)firstObject) compare:((NSDate *)secondObject)];
}];

i tried to put my array on it, but not worked.
Anyone can help me how actully best way to sort nsmutablearray contain nsdate. I want to put the latest date at top of the index array.

So the real question is, how can i sort data inside arrDate?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:46:48+00:00Added an answer on June 3, 2026 at 1:46 pm

    Your date format does not match the date strings you are using.

    yyyy-MM-dd hh:mm:ss
    

    means the dates you supply should look something like:

    2012-05-09 15:54:21
            ^^ Day
         ^^ Month
    

    i.e. your strings are missing the hours, minutes and seconds and you have some dates with month 22 and month 15. If a date formatter can’t parse a date, I think it returns nil which would mean you are trying to put nil into an array, which will cause an exception.

    Edit

    To actually sort the dates, your sample code looks OK except that you want most recent first, so reverse the compare:

    NSArray *sortedArray = [arrDate sortedArrayUsingComparator:^(id firstObject, id secondObject) {
         return [secondObject compare: firstObject];
    }];   
    

    Or with a mutable array, you can sort in place:

    [arrDate sortUsingComparator:^(id firstObject, id secondObject) {
         return [secondObject compare: firstObject];
    }];    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got a problem with a query I'm trying to write. I have a table
I have got problem with my shaders. I am trying to put textures and
I have a problem with Gridview sorting that is similar to others but I'm
I have a very specific problem here. I have a multi-dimensional array that I
I've got problem with sorting my lucene.net index in .NET. I tried almost every
I've got a problem with my SQL. I have a table (named Regions) looking
I've got a little problem with an exercise I have to finish. We have
I've got a blocking problem with the sorting functionality of a JTable; this made
I have got problem with my idea on andriod app. I'd like to play
I've got problem with sorting entries in JqGrid. Orderby seem to not work. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.