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 6855787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:48:07+00:00 2026-05-27T01:48:07+00:00

I have a NSArray made out of numbers 1..50, which represents a table with

  • 0

I have a NSArray made out of numbers 1..50, which represents a table with columns & rows.

I need to reverse only the order of the columns, while keeping the order of the rows.
So for example:

0,1,2,3,4,5,6
7,8,9,9,10,11,12

has to be

6,5,4,3,2,1,0
12,11,10,9,8,7

Right now, i use a huge IF statement for that:

for (dd *d in dates[i]) {
  if (tileNum==0) {
    reversedTileNum = 6;
  } else if (tileNum==1) {
    reversedTileNum = 5;
  }else if (tileNum==2) {
    reversedTileNum = 4;
  }else if (tileNum==3) {
    reversedTileNum = 3;
  }else if (tileNum==4) {
    reversedTileNum = 2;
  }else if (tileNum==5) {
    reversedTileNum = 1;
  } else if (tileNum==6) {
    reversedTileNum = 0;
  }
  ....
  .... 
}
  • 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-27T01:48:08+00:00Added an answer on May 27, 2026 at 1:48 am

    Here’s a solution that should be easy to drop into any project. It involves two categories: one on NSMutableArray that provides a method to swap objects at two indices, and one on NSArray that provides the -arrayByReversingGroups: method. The idea is to swap the elements in pairs within a group, reversing the group. If number of elements in the array isn’t an even multiple of groupSize, the extras at the end are left untouched.

    The code presented here is a complete program, so you can see an example of using -arrayByReversingGroups: in the main() function.

    #import <Foundation/Foundation.h>
    
    @interface NSArray(Reversible)
    
    -(NSArray*)arrayByReversingGroups:(int)groupSize;
    
    @end
    
    @interface NSMutableArray(Swappable)
    
    -(void)swapObjectAtIndex:(int)first withObjectAtIndex:(int)second;
    
    @end
    
    @implementation NSArray(Reversible)
    
    -(NSArray*)arrayByReversingGroups:(int)groupSize
    {
        NSMutableArray *newArray = [self mutableCopy];
        // Iterate over the array in chunks of groupSize elements. i will be first index in
        // the current chunk.
        for (int i = 0; (i + groupSize) < [newArray count]; i += groupSize) {
            // Iterate over the items in the current chunk, swapping the bth and
            // (groupsize-b-1)th elements until they meet at groupsize/2.
            for (int b = 0; b <= (groupSize / 2); b++) {
                int first = i + b;
                int second = i + groupSize - b - 1;
                [newArray swapObjectAtIndex:first withObjectAtIndex:second];
            }
        }
        return [newArray copy];
    }
    
    @end
    
    @implementation NSMutableArray(Swappable)
    -(void)swapObjectAtIndex:(int)first withObjectAtIndex:(int)second
    {
        id temp = [[self objectAtIndex:second] retain];
        [self replaceObjectAtIndex:second withObject:[self objectAtIndex:first]];
        [self replaceObjectAtIndex:first withObject:temp];
        [temp release];
    }
    
    @end
    
    int main (int argc, const char * argv[])
    {
        @autoreleasepool {
            NSArray *array = [NSArray arrayWithObjects:
                              @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", nil];
            NSLog(@"Original: %@", array);
            NSLog(@"Reversed: %@", [array arrayByReversingGroups:5]);
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table view made up of an NSArray of instances of a
I have a pie chart which is made using UIBezierPath's. I now need those
I have an NSArray of NSDictionary objects which I would like to be able
I have made a question which is a bit similar to this, though the
In my app I have UITableView & table data can be updated from remote
I have an NSArray and I'd like to create a new NSArray with objects
I have an NSArray of Foos in an Objective-C program. I would like to
I have an NSArray of (Product) objects that are created by parsing an XML
If I have an NSArray with some values in them. Is there a way
Example: I have an NSArray with 40 objects. What is the most efficient way

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.