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?