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

  • SEARCH
  • Home
  • 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 4051516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:12:17+00:00 2026-05-20T14:12:17+00:00

For the purpose of asking this question about ordering. The following MyObject class returns

  • 0

For the purpose of asking this question about ordering. The following MyObject class returns an instance with random generated category names.

I use the following dataSource methods:

numberOfSections accessed with [dataSource count].

titleForSection accessed with [[dataSource objectAtIndex:indexPath.section] valueForKey:@"categoryName"].

numberOfRowsInSection accessed with [[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] count].

And finally, the MyObject for each row is accessed with [[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] objectAtIndex:indexPath.row] on the cellForRowAtIndexPath method.

I use the following code to create a dataSource that displays 9 section categories, however I’m a little stuck on the ordering of these categories and the data within. Assume there’s an NSDate property as part of the MyObject class.

Question: How would I go about using this to display the records in descending order?

- (void)createDatasource
{
    NSInteger numberOfObjects = 10;
    NSMutableArray *objects = [NSMutableArray arrayWithCapacity:numberOfObjects];
    NSMutableArray *categories = [NSMutableArray arrayWithCapacity:numberOfObjects];
    for (int i = 0; i < numberOfObjects; i++)
    {
        MyObject *obj = [[MyObject alloc] init];
        [objects addObject:obj];
        [categories addObject:obj.category];
        [obj release];
    }
    NSSet *set = [NSSet setWithArray:categories];
    NSMutableArray *dataSource = [[NSMutableArray alloc] initWithCapacity:[set count]];
    for (NSString *categoryString in set)
    {
        NSMutableDictionary *mainItem = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil, @"categoryName", nil, @"myObjects", nil];
        NSMutableArray *mainItemMyObjects = [NSMutableArray array];
        [mainItem setValue:categoryString forKey:@"categoryName"];
        for (MyObject *obj in objects)
        {
            if ([obj.category isEqualToString:categoryString])
            {
                [mainItemMyObjects addObject:obj];
            }
        }
        [mainItem setValue:mainItemMyObjects forKey:@"myObjects"];
        [dataSource addObject:mainItem];
        [mainItem release];
    }
    NSLog (@"objects = %@\ncategories = %@\nset = %@\ndatasource = %@", objects, categories, set, dataSource);
}
  • 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-05-20T14:12:18+00:00Added an answer on May 20, 2026 at 2:12 pm

    Easiest would be to sort your arrays, using NSMutableArray’s sorting mutators or NSArray’s sorting methods. Otherwise you’d have to construct some sort of mapping from input indices to dataSource indices for use by the various data source methods.


    Edit Requested sample code for sorting, something like this should work. I assume you are wanting to sort everything by a property named date on the MyObject.

    // First, sort the myObject mutable array in each category
    for (NSDictionary *d in dataSource) {
        [[d valueForKey:@"myObjects"] sortUsingComparator:^NSComparisonResult(id o1, id o2){
            // Compare dates. NSDate's 'compare:' would do ascending order, so if we just
            // reverse the order of comparison they'll come out descending.
            return [[o2 date] compare:[o1 date]];
        }];
    }
    
    // Second, sort the categories by the earliest dated object they contain
    [dataSource sortUsingComparator:^NSComparisonResult(id o1, id o2){
        // Extract the first object from each category's array, which must be the
        // earliest it contains due to the previous sort.
        MyObject *myObject1 = [[o1 valueForKey:@"myObjects"] objectAtIndex:0];
        MyObject *myObject2 = [[o2 valueForKey:@"myObjects"] objectAtIndex:0];
    
        // Compare dates, as above.
        return [[myObject2 date] compare:[myObject1 date]];
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly apologies for the length of this question, and for asking about the Facebook
I couldn't find a question that would fit this purpose, so I'm asking it.
I've tried to simplify for the purposes of asking this question. Hopefully, this will
Sorry for asking it again, there are already some questions about this keyword. But
Sorry for asking this question, but I searched all Java-related questions, but I got
I'm asking this question given my chosen development frameworks of JPA (Hibernate implementation of),
First of all I alwyas use one #ID. just asking this question to know
Before answering this question, understand that I am not asking how to create my
Since this question is about the increment operator and speed differences with prefix/postfix notation,
This question is from a C# guy asking the C++ people. (I know a

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.