iOSDevp
iOSDevp

Reputation: 195

Sort Descriptor not working in ios

i used the Sort Descriptor to Sort the NSMutableArray By one & multiple Values,First i tried by to sort By Price,it sort in some other Order here is my code help me, My i create the Dictionary by below code and added to the NSMutableArray

    for(int i=0;i<[priceArray count];i++)
    {
        cellDict=[[NSMutableDictionary alloc]init];
        [cellDict setObject:nameArray[i] forKey:@"Name"];
        [cellDict setObject:splPriceArray[i] forKey:@"Percentage"];
        [cellDict setObject:priceArray[i] forKey:@"Price"];
        [resultArray addObject:cellDict];
    }
// To Sort in Ascending Order
    NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Price" ascending:YES];
    NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
    NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];

    NSLog(@"Result %@ Sorted arr %@",resultArray, sortedArray);

And Output is:

 Result (
        {
        Name = "Black Eyed Peas";
        Percentage = 0;
        Price = 80;
    },
        {
        Name = "Black Gram";
        Percentage = 0;
        Price = 56;
    },
        {
        Name = "Channa White";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Double Beans";
        Percentage = 0;
        Price = 95;
    },
        {
        Name = "Gram Dall";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Green Moong Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Ground Nut";
        Percentage = 0;
        Price = 140;
    },
        {
        Name = "Moong Dal";
        Percentage = 0;
        Price = 75;
    },
        {
        Name = "Orid Dal";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Toor Dal";
        Percentage = 0;
        Price = 150;
    }
) Sorted arr (
        {
        Name = "Channa White";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Gram Dall";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Orid Dal";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Ground Nut";
        Percentage = 0;
        Price = 140;
    },
        {
        Name = "Green Moong Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Toor Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Black Gram";
        Percentage = 0;
        Price = 56;
    },
        {
        Name = "Moong Dal";
        Percentage = 0;
        Price = 75;
    },
        {
        Name = "Black Eyed Peas";
        Percentage = 0;
        Price = 80;
    },
        {
        Name = "Double Beans";
        Percentage = 0;
        Price = 95;
    }
)

Here The Sorted Array Sorting in some other Order I want to sort this in Ascending order by price.

Upvotes: 0

Views: 264

Answers (1)

charlyatwork
charlyatwork

Reputation: 1197

It's unclear what your test data looks like - but the following snippet works as expected

NSArray *priceArray = [NSArray arrayWithObjects:@(74),@(100),@(100),@(130), nil];
    NSArray *nameArray = [NSArray arrayWithObjects:@"Yva",@"Hallo", @"Adam", @"Xavier", nil];

NSMutableArray *resultArray = [NSMutableArray new];
for(int i=0;i<[priceArray count];i++)
{
    NSMutableDictionary *cellDict=[[NSMutableDictionary alloc]init];
    [cellDict setObject:nameArray[i] forKey:@"Name"];
    [cellDict setObject:priceArray[i] forKey:@"Percentage"];
    [cellDict setObject:priceArray[i] forKey:@"Price"];
    [resultArray addObject:cellDict];
}
// Sort by Name
//NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Price" ascending:YES];

// Sort by Name
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];

NSLog(@"Result %@ Sorted arr %@",resultArray, sortedArray);

Result:

2015-07-11 12:54:54.358 ret[10480:162783] Result (
        {
        Name = Yva;
        Percentage = 74;
        Price = 74;
    },
        {
        Name = Hallo;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Adam;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Xavier;
        Percentage = 130;
        Price = 130;
    }
) Sorted arr (
        {
        Name = Adam;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Hallo;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Xavier;
        Percentage = 130;
        Price = 130;
    },
        {
        Name = Yva;
        Percentage = 74;
        Price = 74;
    }
)

Upvotes: 1

Related Questions