sorting - Sort Descriptor not working in ios -


i used sort descriptor sort nsmutablearray 1 & multiple values,first tried sort price,it sort in other order here code me, create dictionary below code , added 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];     } // 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 sorted array sorting in other order want sort in ascending order price.

it's unclear test data looks - following snippet works 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 name //nssortdescriptor *sort =[[nssortdescriptor alloc] initwithkey:@"price" ascending:yes];  // sort 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;     } ) 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -