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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:14:59+00:00 2026-05-27T02:14:59+00:00

So i have an array that i retrieve from a web service in no

  • 0

So i have an array that i retrieve from a web service in no particular order

example:

0 => x large, 
1 => large, 
2 => XX large, 
3 => small,
4 => medium, 
5 => x small

I need to sort them: firstly based on specific – which could be reverse alphabetic:

small
medium
large

Secondly i need to sort them based on their ‘x’ counter parts:

x small
small
medium
large
x large
xx large

I know i can do this with brute force string matching but i would really like a suggestion on how to do this tidily, perhaps a regex or something more elegant?

  • 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-27T02:15:00+00:00Added an answer on May 27, 2026 at 2:15 am

    Use NSComparator block syntax. Something like

    NSArray * sizes = [NSArray arrayWithObjects:  @"x small",@"small",@"medium",@"large",@"x large", nil];
    
    NSArray *sortedBySizes =[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([sizes indexOfObject:[obj1 size]]> [sizes indexOfObject:[obj2 size]])
            return (NSComparisonResult)NSOrderedAscending;
        if ([sizes indexOfObject:[obj1 size]]< [sizes indexOfObject:[obj2 size]])
            return (NSComparisonResult)NSOrderedDescending;
        return (NSComparisonResult)NSOrderedSame;
    }];
    

    In the second approach I added a mapping between the numbers send by the web server and the x-sizes. Now [obj size]; is suppose to return a NSNumber object.

    NSArray * sizesStrings = [NSArray arrayWithObjects:  @"x small",@"small",
                                                         @"medium",@"large",
                                                         @"x large",@"xx large", 
                                                         nil];
    NSArray * sizesNumbers = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
                                                       [NSNumber numberWithInt:3],
                                                       [NSNumber numberWithInt:4],
                                                       [NSNumber numberWithInt:1],
                                                       [NSNumber numberWithInt:0],
                                                       [NSNumber numberWithInt:2], 
                                                       nil];
    
    NSDictionary *sizes = [NSDictionary dictionaryWithObjects:sizesStrings 
                                                       forKeys:sizesNumbers];
    
    NSArray *sortedBySizes = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString *sizeObj1String = [sizes objectForKey:[obj1 size]];
        NSString *sizeObj2String = [sizes objectForKey:[obj1 size]];
    
        int i1 = [sizesStrings indexOfObject:sizeObj1String];
        int i2 = [sizesStrings indexOfObject:sizeObj2String];
    
        if (i1 > i2)
            return (NSComparisonResult)NSOrderedAscending;
        if (i2 > i1)
            return (NSComparisonResult)NSOrderedDescending;
        return (NSComparisonResult)NSOrderedSame;
    }];
    

    The second task of the question — the grouping into small, medium, large — could be done like this:

    NSDictionary *groups = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSMutableArray array],[NSMutableArray array],[NSMutableArray array], nil] 
                                        forKeys:[NSArray arrayWithObjects:@"small",@"medium",@"large",nil]
                            ];
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        int i = [[obj size] intValue];
        if (i == 5 || i == 3) 
            [[groups objectForKey:@"small"] addObject:obj];
        else if (i == 2 || i == 0 || i == 1)
            [[groups objectForKey:@"large"] addObject:obj];
        else
            [[groups objectForKey:@"medium"] addObject:obj];
    
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that I'm trying to retrieve an array of data from,
I have JavaScript code that retrieves numerical vectors from a web-service. The original data
I have an array that I want to sort based on the value of
I am trying to retrieve information from a web service call. The following is
Example: I have a list of friends which I retrieve as XML from a
I have a Java JDBC application that uses multiple threads to retrieve information from
I have a PHP array of IDs of rows to retrieve from a MySQL
I have an array that contains a list of vertices which I copy to
I have an array that looks like: $fields = array( f1 => array(test), f2
I have an array that has keys and values. For eg: Array ( [name]

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.