3

There is a NSSet which contains some size strings for products. I am trying to use the NSSortDescriptor to sort the array. since it sorts alphabetically, the result is not exactly right.

The code snippet is as below:

NSSet *set = [[NSSet alloc] initWithObjects:@"40", @"30", @"31", @"3XL", @"44", @"46", @"50", @"52", @"54", @"56", @"48", @"33", @"L", @"M", @"S", @"XL", @"XS", @"XXS",@"XXL",nil];
NSArray *sizeArray = [set allObjects];
NSSortDescriptor *sizeSort = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending: YES];
NSArray *sortedArray = [sizeArray sortedArrayUsingDescriptors:@[sizeSort]];

NSLog(@"Set: %@ \n Sorted: %@", set, sortedArray);

The output is as follow:

Set: {( 46, 52, XL, XXS, 54, 48, 33, 44, 50, L, 40, M, S, 30, 56, XS, XXL, 3XL, 31 )} Sorted: ( 30, 31, 33, 3XL, 40, 44, 46, 48, 50, 52, 54, 56, L, M, S, XL, XS, XXL, XXS )

The desirable result should be (30, 31, 33, 40, 44, 46, 48, 50, 52, 54, 56, XXS, XS, S, M, L, XL, XXL, 3XL )

Is there any better way to sort like this?

2 Answers 2

4
NSSet *set = [[NSSet alloc] initWithObjects:@"40", @"30", @"31", @"3XL", @"44", @"46", @"50", @"52", @"54", @"56", @"48", @"33", @"L", @"M", @"S", @"XL", @"XS", @"XXS",@"XXL",nil];
NSArray *sizeArray = [set allObjects];

    NSArray *sizes = @[@"XXS", @"XS", @"S", @"M", @"L", @"XL", @"XXL", @"3XL"];
    NSArray *sortedArray = [sizeArray sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        NSUInteger index1 = [sizes indexOfObject:obj1];
        NSUInteger index2 = [sizes indexOfObject:obj2];
        if (index1 == NSNotFound && index2 == NSNotFound) {
            return [@([obj1 intValue]) compare:@([obj2 intValue])];
        }
        else if (index1 == NSNotFound)
        {
            return NSOrderedAscending;
        }
        else if (index2 == NSNotFound)
        {
            return NSOrderedDescending;
        }
            return [@(index1) compare:@(index2)];
    }];

NSLog(@"Set: %@ \n Sorted: %@", set, sortedArray);

Output:

Set: ( 46, 52, XL, XXS, 54, 48, 33, 44, 50, L, 40, M, S, 30, 56, XS, XXL, 3XL, 31 )
Sorted: (30, 31, 33, 40, 44, 46, 48, 50, 52, 54, 56, XXS, XS, S, M, L, XL, XXL, 3XL )
Sign up to request clarification or add additional context in comments.

Comments

1

Updated answer...

NSSet *set = [[NSSet alloc] initWithObjects:@"40", @"30", @"31", @"3XL", @"44", @"46", @"50", @"52", @"54", @"56", @"48", @"33", @"L", @"M", @"S", @"XL", @"XS", @"XXS",@"XXL",nil];
NSArray *sizeArray = [set allObjects];

NSMutableArray *numberArray, *charArray;
numberArray = [NSMutableArray array];
charArray = [NSMutableArray array];

BOOL valid;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet; 

for (int i = 0 ; i < [sizeArray count]; i++)
{
    inStringSet = [NSCharacterSet characterSetWithCharactersInString:[sizeArray objectAtIndex:i]];
    valid = [alphaNums isSupersetOfSet:inStringSet];    

    if (valid)
        [numberArray addObject:[sizeArray objectAtIndex:i]];
    else
        [charArray addObject:[sizeArray objectAtIndex:i]];
}



NSSortDescriptor *sizeSort = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending: YES];
NSSortDescriptor *sizeSort1 = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending: NO];

NSArray *sortedArray1 = [numberArray sortedArrayUsingDescriptors:@[sizeSort]];
NSArray *sortedArray2 = [charArray sortedArrayUsingDescriptors:@[sizeSort1]]; // sort in descending order.

NSMutableArray *fullSortedArray = [NSMutableArray array];

[fullSortedArray addObjectsFromArray:sortedArray1];
[fullSortedArray addObjectsFromArray:sortedArray2];

    NSLog(@"Set = %@ \n Sorted = %@", set, fullSortedArray);

Hi check out this

OutPut:

Set = {( 46, 52, XL, XXS, 54, 48, 33, 44, 50, L, 40, M, S, 30, 56, XS, XXL, 3XL, 31 )} Sorted = ( 30, 31, 33, 40, 44, 46, 48, 50, 52, 54, 56, 3XL, L, M, S, XL, XS, XXL, XXS )

4 Comments

the last part should be XXS, XS, S, M, L, XL, XXL, 3XL
@Chancy: What type of sort this.. there is no order in this.\
Yes, there are some free typing strings in the array.
@Chancy: I updated my answer. If you need any free typing use for loop and change order of the objects in sortedArray2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.