iphone - How to sorting core data fetch result -
hi need sort fetch result deasending order here code
nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; nserror *error1; nsentitydescription *entitydesc; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; entitydesc=[nsentitydescription entityforname:@"subcategoryentity" inmanagedobjectcontext:context]; [fetchrequest setentity:entitydesc]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"subcategoryid" ascending:no]; [fetchrequest setsortdescriptors:[nsarray arraywithobject:sortdescriptor]]; [sortdescriptor release]; nsarray *array = [context executefetchrequest:fetchrequest error:&error1]; here use "subcategory" string type show me correct order in "single digits" couldn't work in "double digits"
here order after count "11" "9","8","7","6","5","4","3","2","1","10","0"
here need show "10","9","8","7","6","5","4","3","2","1","0"
i don't why it's happing can 1 me
thanks advance.
you're getting order in way because how sorting of strings work. can try using nssortdescriptor custom comparesubcategoryid: selector in custom subcategory class subcategoryentity.
update
initialize sort descriptor this:
nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"subcategoryid" ascending:no selector:@selector(comparesubcategoryid:)]; then add method custom nsmanagedobject subclass:
- (nscomparisonresult)comparesubcategoryid:(id)otherobject { int ownsubcatid = [[self subcategoryid] intvalue]; int othersubcatid = [[otherobject subcategoryid] intvalue]; if (ownsubcatid < othersubcatid) return nsorderedascending; if (ownsubcatid > othersubcatid) return nsordereddescending; return nsorderedsame; }
Comments
Post a Comment