Issue with Sorting NSMutableArray - ios

I have an NSMutableArray which contains an NSDictionary, I need to sort the array based on certain criteria.
Sorting criteria is
sort based on properties.property.value, suppose if I want to sort based on properties.property.name = Risk value. I want all the elements with Risk value first with the value in Ascending order. That means for the Dictionary which doesn't have
properties.property.name = Risk must come last. Some of the dictionaries don't have these names.
Please help...
finalArr (
{
ViewTag = 101;
"action-taken-date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Risk;
name = Risk;
value = "3 - Low";
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 102;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Risk;
name = Risk;
value = "2 - Low";
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 103;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Status;
name = Status;
value = "Pending Signoffs";
},
{
label = Priority;
name = Priority;
value = 3;
}
);
};
{
ViewTag = 104;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
property = (
{
label = Priority;
name = priority;
value = 1;
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 103;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
property = (
{
label = Status;
name = Status;
value = "Pending Signoffs";
},
{
label = Priority;
name = Priority;
value = 2;
}
);
};
)

Try with this
NSString *risk = #"Risk";
[finalArray sortUsingComparator:^NSComparisonResult(NSDictionary * dict1, NSDictionary * dict2) {
NSDictionary *property1 = dict1[#"properties"][#"property"][0];
NSString *name1 = property1[#"name"];
NSDictionary *property2 = dict2[#"properties"][#"property"][0];
NSString *name2 = property2[#"name"];
if ([name1 isEqualToString:name2]) {
//if both names are equal then they are sorted based on value
NSString *value1 = property1[#"value"];
NSString *value2 = property2[#"value"];
return [value1 compare:value2 options:NSNumericSearch];
}else if ([name1 isEqualToString:risk]){
//if name1 is equal to risk it is moved up
return NSOrderedAscending;
}else{
return NSOrderedDescending;
}
}];

It sounds like you have multiple different requirements (logic) which go into determining the desired sort result. You should write your own comparison logic and use one of the array methods like sortUsingComparator:.

Related

How to merge 2 Arrays by comparing two different objects having same values ?

I have 2 Arrays named "onGoingBookings" and "sortedMSArray" . I want to inject / merge sortedMSArray (status key only) to onGoingBooings Array (main Array) when both booking id values are same.
Response of OngoingBookings
{
addrLine1 = "Al Thanyah Fifth, Dubai, United Arab Emirates";
addrLine2 = " ";
amount = "";
apntDate = "2017-12-11";
apntDt = "2017-12-11 11:48:18";
apntTime = "11:48 am";
apptLat = "25.071571350098";
apptLong = "55.14294052124";
bid = 374;
bookType = 1;
cancelAmount = 30;
"cancel_reason" = "";
"cat_name" = "Car Wash";
"dispute_msg" = "";
disputed = "";
dt = 20171211114818;
email = "dev#ios.com";
fdata = {
};
fname = Apple;
lname = Mob;
notes = "";
pPic = "https://s3.amazonaws.com/iserve/ProfileImages/dev#ios.com2017-11-2711:34:26.jpg";
phone = 561462146;
pid = 48;
"star_rating" = 0;
statCode = 6;
status = "Job started.";
},
{
addrLine1 = "Al Thanyah Fifth, Dubai, United Arab Emirates";
addrLine2 = " ";
amount = "";
apntDate = "2017-12-06";
apntDt = "2017-12-06 19:29:14";
apntTime = "07:29 pm";
apptLat = "25.071369171143";
apptLong = "55.143840789795";
bid = 354;
bookType = 2;
cancelAmount = 50;
"cancel_reason" = "";
"cat_name" = Plumbing;
"dispute_msg" = "";
disputed = "";
dt = 20171206192914;
email = "";
fdata = {
};
fname = "";
lname = "";
notes = "";
pPic = "";
phone = "";
pid = "";
"star_rating" = 0;
statCode = 1;
status = "Job Pending";
},
{
addrLine1 = "Al Thanyah Fifth, Dubai, United Arab Emirates";
addrLine2 = " ";
amount = "";
apntDate = "2017-12-06";
apntDt = "2017-12-06 19:24:12";
apntTime = "07:24 pm";
apptLat = "25.071369171143";
apptLong = "55.143840789795";
bid = 353;
bookType = 1;
cancelAmount = 30;
"cancel_reason" = "";
"cat_name" = "Car Wash";
"dispute_msg" = "";
disputed = "";
dt = 20171206192412;
email = "";
fdata = {
};
fname = "";
lname = "";
notes = "";
pPic = "";
phone = "";
pid = "";
"star_rating" = 0;
statCode = 1;
status = "Job Pending";
},
Response of sortedMSArray
{
bookingid = 325;
status = 1;
},
{
bookingid = 333;
status = 3;
},
{
bookingid = 374;
status = 3;
}
I need Following Solution
Step 1 - compare both array by using #"bid"(ongoingbookings) and #"bookingid"(sortedMSArray)
Step 2 - merge the status key when both booking ids are same.
Please post a code snipped according to my code and response.
Reference Codes
if(requestType == RequestTypeGetAllApptDetails)
{
if(pageIndex == 0)
{
pastBookings = [[NSMutableArray alloc]init];
onGoingBookings = response[#"ongoing_appts"];
NSLog(#"CHECKINGFAST%#",onGoingBookings);
}
[pastBookings addObjectsFromArray:response[#"past_appts"]];
self.tableView.backgroundView = self.messageView;
[self.tableView reloadData];
}
else if(requestType == RequestTypeGetReportMaterialStatus)
{ // NSArray *custStatusTempArray = response;
customerStatusArray = response;
customerstatusDict = response[#"customerstatus"];
NSLog(#"CUSTOMERSTATUSARRAY %#", customerStatusArray);
NSLog(#"CUSTOMERSTATUSDICT %#", customerstatusDict);
if(customerStatusArray == nil || [customerStatusArray count]==0)
{
NSLog(#"NO MATERIAL ACCEPTED");
}
else
{
custStatusFilteredArray = [NSMutableArray array];
if([customerstatusDict isKindOfClass:[NSDictionary class]])
{
NSDictionary *dict = (NSDictionary *)customerstatusDict;
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[custStatusFilteredArray addObject:obj];
NSLog(#"FILTEREDARRAY%#",custStatusFilteredArray);
}];
}
materialStatusStringArray = [custStatusFilteredArray copy];
NSLog(#"materialStatusStringarray%#",materialStatusStringArray);
//sorting the materialstatus string array
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"bookingid" ascending:NO];
sortedMSArray = [materialStatusStringArray sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"Sorted Array Response -%#", sortedMSArray);

Depending up on json values how we have to move view controller from one view to another view?

JSON OUTPUT:
category = (
{
"category_id" = 1;
column = 0;
description = "";
image = "images/Car Wash.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = "Car Wash";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
{
"category_id" = 6;
column = 0;
description = "";
image = "images/hatchpack.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = hatchpack;
"parent_id" = 1;
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
}
);
top = "";
},
{
"category_id" = 2;
column = 0;
description = "";
image = "images/Pest Control.png";
"meta_description" = "";
"meta_keyword" = "";
name = "Pest Control";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
},
{
"category_id" = 3;
column = 0;
description = "";
image = "images/Electrician.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = Electrician;
"parent_id" = "";
"sort_order" = 0;
status = "";
subcategory = (
);
top = "";
},
{
"category_id" = 4;
column = 0;
description = "";
image = "images/Washing Machine.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = "Washing Machine";
"parent_id" = "";
"sort_order" = 0;
status = "";
subcategory = (
);
top = "";
},
{
"category_id" = 5;
column = 0;
description = "";
image = "images/TV Repair.png";
"meta_description" = "";
"meta_keyword" = "";
name = "TV Repair";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
},
{
"category_id" = 6;
column = 0;
description = "";
image = "images/hatchpack.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = hatchpack;
"parent_id" = 1;
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
}
);
success = 1;
}
MY CODE:
for(NSDictionary *DicHoleCategories in ArrCategory)
{
[DicAllValues setObject:[DicHoleCategories objectForKey:#"subcategory"] forKey:#"subcategory"];
[ArrName addObject:DicAllValues];
}
//did select row at indexpath method//
subCategory = [[ArrName objectAtIndex:indexPath.row] objectForKey:#"subcategory"];
if(subCategory==nil||[subCategory isEqual:#""])
{
productScreen *screen = [[productScreen alloc]init];
screen = [self.storyboard instantiateViewControllerWithIdentifier:#"product"];
[self presentViewController:screen animated:YES completion:nil];
}
else
{
CarDetailsVC *car = [[CarDetailsVC alloc]init];
car=[self.storyboard instantiateViewControllerWithIdentifier:#"category"];
[self presentViewController:car animated:YES completion:nil];
}
Here I have mentioned my code properly,getting the subcategory values has been done and I have comparing the values is null or the string is empty,but only else part is working what I have to do please any one help me.
for(NSDictionary *DicHoleCategories in ArrCategory)
{
[DicAllValues setObject:[DicHoleCategories objectForKey:#"subcategory"] forKey:#"subcategory"];
[ArrName addObject:DicAllValues];
}
//did select row at indexpath method//
NSArray *subCategory=[[ArrName objectAtIndex:indexPath.row] objectForKey:#"subcategory"];
if(subCategory.count ==0)//subcategory is an array
{
productScreen *screen=[[productScreen alloc]init];
screen =[self.storyboard instantiateViewControllerWithIdentifier:#"product"];
[self presentViewController:screen animated:YES completion:nil];
}
else
{
CarDetailsVC *car=[[CarDetailsVC alloc]init];
car=[self.storyboard instantiateViewControllerWithIdentifier:#"category"];
[self presentViewController:car animated:YES completion:nil];
}
You have given && condition inside if statement. Both conditions can't be true simultaneously. Moreover, check your string with isEqualToString method.
So put your if condition as given below -
if([subcategory length] ==0){
// Your code goes here.
}

Manipulating NSDictionary data

I've managed to export the below data, multiplied by several hundred, into an NSDictionary. I now need to build out a way to put this data into Tinder style app. Can anyone advise how I can get my app to pull out the data against Key "Title" for various indexes. Currently it pulls out 45 records of data containing that one Title as there are 45 keys in my data set.
{
AttachmentFiles = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/AttachmentFiles";
};
};
Attachments = 0;
AuthorId = 22;
AverageRating = "<null>";
BestAnswerId = "<null>";
Body = "We need to think xyz";
CategoriesLookupId = 16;
Community = "<null>";
ContentType = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/ContentType";
};
};
ContentTypeId = 0x01200200C5BB1FEE2601B9439CD527C288D85A7B;
Created = "2014-03-21T08:37:33Z";
DescendantLikesCount = 8;
DescendantRatingsCount = "<null>";
DiscussionLastUpdated = "2014-06-19T12:57:58Z";
EditorId = 96;
EmailSender = "<null>";
FieldValuesAsHtml = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FieldValuesAsHtml";
};
};
FieldValuesAsText = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FieldValuesAsText";
};
};
FieldValuesForEdit = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FieldValuesForEdit";
};
};
File = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/File";
};
};
FileSystemObjectType = 1;
FilterCategory = "<null>";
FirstUniqueAncestorSecurableObject = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FirstUniqueAncestorSecurableObject";
};
};
Folder = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/Folder";
};
};
GUID = "28144fd4-84b0-434b-9e99-01d939e6a773";
Has20likes = "<null>";
ID = 3;
Id = 3;
IsFeatured = 0;
IsIdeaCreated = "<null>";
IsQuestion = 0;
LastReplyById = 49;
LikesCount = 5;
MemberLookupId = 7;
Modified = "2015-03-11T15:25:20Z";
"OData__UIVersionString" = "2.0";
ParentItemEditorId = "<null>";
ParentItemID = "<null>";
ParentList = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/ParentList";
};
};
Popularity = "7.23632608186099";
RatingCount = "<null>";
RoleAssignments = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/RoleAssignments";
};
};
StepChangePillar = "<null>";
Title = "JIRA Governance";
"__metadata" = {
etag = "\"14\"";
id = "Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)";
type = "SP.Data.Community_x0020_DiscussionListItem";
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)";
};
},
Thanks
You should have an array of dictionaries correct?
If so this should work:
NSInteger index = 0; //You can set this to the current page of your scroll view
NSArray * items = /* YOUR ARRAY OF DICTIONARIES */;
NSDictionary * dict = [items objectAtIndex:index];
NSString * title = [dict objectForKey:#"Title"];
You might also consider making a subclass of NSObject and building in a parser for your dictionary so you can access the variables with strong types.

iphone sdk+How to retrive dictionary inside array of dictionary values

i want to retrive KpiName key(KpiData is array inside kpiName is dictionary key) and my data structure mentioned below
--Array of dictionaries inside array of dictinary
ex..listOfProjectsArray--(
{
alertSeverity = 1;
customerName = TPMG;
endDate = "05-05-2013";
isProjectHide = 1;
kpiData = (
{
KpiName = "Change Request ";
kpiActual = 14;
kpiPlanned = "";
kpiUnit = "";
},
{
KpiName = "Aged Debit";
kpiActual = 24000;
kpiPlanned = "";
kpiUnit = EUR;
},
);
lastInvoiceDate = "05-04-2013";
nextBillingDate = "05-04-2013";
nextMilestoneDate = "10-04-2013";
nextReviewDate = "08-04-2013";
plannedCompletionDate = "04-05-2013";
projectID = 3000;
projectName = "Rain Harvesting";
projectSortType = "";
projectStatus = 1;
startDate = "01-01-2013";
},
i tried this one but its returning null
NSString *kpiName = [[[[listOfProjectsArray objectAtIndex:indexPath.row] valueForKey:#"kpiData"] objectAtIndex:0]valueForKey:#"kpiName"];
NSLog(#"kpiname:%#", kpiName);
Try this
NSString *kpiName =[[[[listOfProjectsArray objectAtIndex:indexPath.row] objectForKey:#"kpiData"]objectAtIndex:0]objectForKey:#"KpiName"];
I was mentioned wrong dictionary key
NSString *kpiName = [[[[listOfProjectsArray objectAtIndex:indexPath.row] valueForKey:#"kpiData"] objectAtIndex:0]valueForKey:#"kpiName"];
NSLog(#"kpiname:%#", kpiName);
correct one is KpiName key instead of kpiName.

How to get all the photos when user uploads 2 or more in Facebook for iOS

{
comments = {
count = 0;
};
"created_time" = "2013-01-23T06:12:43+0000";
from = {
id = 1171029418;
name = "Ilse Anhy";
};
icon = "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif";
id = "1171029418_10200426347839190";
link = "https://www.facebook.com/photo.php?fbid=10200426378279951&set=a.3604514069432.2168112.1171029418&type=1&relevant_count=2";
"object_id" = 10200426378279951;
picture = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/265145_10200426378279951_1504367216_s.jpg";
privacy = {
value = "";
};
"status_type" = "added_photos";
story = "Ilse Anhy added 2 new photos.";
"story_tags" = {
0 = (
{
id = 1171029418;
length = 9;
name = "Ilse Anhy";
offset = 0;
type = user;
}
);
};
type = photo;
"updated_time" = "2013-01-23T06:12:43+0000";
}
In the "story" object it says "Ilse Anhy added 2 new photos."
In the "picture" object it only gives a reference to a single photo. How would I be able to retrieve the other photo?

Resources