adding array in NSDictionary and ordering it [duplicate] - ios

This question already has answers here:
how to change order of NSMutable array in same way another mutable arrays get changed
(5 answers)
Closed 9 years ago.
i have three array like below
names birthdate remanning
"Abhi Shah", "01/14", 300
"Akash Parikh", "12/09/1989", 264
"Anand Kapadiya", "12/01", 256
"Annabella Faith Perez", "03/02", 347
"Aysu Can", "04/14/1992", 25
"Chirag Pandya" "10/07/1987" 201
plz it will be great help if you give me code for how to add this three array in NSDictionary and then after ordering(ascending) whole dictionary in accordance to "remaning" array
Note in Dic everything should get changed. not only remaning array. names and birthdate should get changed in same way remaning days are getting changed
thank you so much in advance

I would advise you to change the design of your project and create a model for same as having properties :
#interface YourModel : NSObject
#property (strong) NSString *name;
#property (strong) NDDate *birthDate;
#property NSInteger remaining;
#end
And then create an NSMutableArray in your class, and go on to add them.
This will make your work easier, as searching, sorting, filtering , than handling 3 parallel arrays.

You have to take each record (names, birthdate, remanning ) in the dictionary or any structure. And array of that dictionary should be created. To sort the array as per your requirement any sorting mechanism can be used.
-(void)sort
{
//This is the array of dictionaries, where each dictionary holds a record
NSMutableArray * array;
//allocate the memory to the mutable array and add the records to the arrat
// I have used simple bubble sort you can use any other algorithm that suites you
//bubble sort
//
for(int i = 0; i < [array count]; i++)
{
for(int j = i+1; j < [array count]; j++)
{
NSDictionary *recordOne = [array objectAtIndex:i];
NSDictionary *recordTwo = [array objectAtIndex:j];
if([[recordOne valueForKey:#"remaining"] integerValue] > [[recordTwo valueForKey:#"remaining"] integerValue])
{
[array xchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
//Here you get the sorted array
}
Hope this helps.

If you use the design proposed by Anoop, the sorting code using blocks would be something similar to this:
NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSInteger first = [(YourModel*)a remaining];
NSInteger second = [(YourModel*)b remaining];
return [first compare:second];
}];

Related

Strange dictionary sort ios objective c

I want to do kind of a weird dictionary sort. I have non-unique values and keys and get something like this
NSArray *counts = [#"1",#"2",#"2",#"3",#"6",#"10"];
NSArray *names =[#"Jerry",#"Marge",#"Jerry",#"Marge",#"Jen",#"Mark"];
The output that I want is an descending ordered list by counts with unique names. I don't want lower values of the same person in my outputted arrays. The output should be.
sortedNames=[#"Mark",#"Jen",#"Marge",#"Jerry"]
sortedCounts=[#"10",#"6",#"3",#"2"];
I would really appreciate some help on this.
NSMutableArray *userNameArray = [[NSMutableArray alloc] init];
NSMutableArray *countArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in bigDick) {
NSString *nameString =[dict objectForKey:#"Name"];
NSString *countString =[dict objectForKey:#"Count"];
NSInteger countInt = [countString integerValue];
NSNumber *countNumber =[NSNumber numberWithInt:countInt];
[userNameArray addObject:nameString];
[countArray addObject:countNumber];
}
NSArray *namesAscending =[[userNameArray reverseObjectEnumerator]allObjects];
NSArray *countsAscending=[[countArray reverseObjectEnumerator]allObjects];
// Put the two arrays into a dictionary as keys and values
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:countsAscending forKeys:namesAscending];
// Sort the first array
NSArray *sortedCountArray = [[dictionary allValues] sortedArrayUsingSelector:#selector(compare:)];
// Sort the second array based on the sorted first array
// NSArray *sortedNameArray= [dictionary objectsForKeys:sortedCountArray notFoundMarker:[NSNull null]];
NSMutableArray *nameArray =[[NSMutableArray alloc] init];
for (int i=1; i<sortedCountArray.count; i++) {
NSString *name = [dictionary allKeysForObject:sortedCountArray[i]];
if (sortedCountArray[i]!=sortedCountArray[i-1]) {
[nameArray addObject:name];
}
}
an old method is to manual sort the array with numbers, by searching on every iteraton for the biggest value, and when you find the max value take the name from the other vector at index of the max number and move it in new vector...
max = counts[0];
counter = 0;
for (int i=0;i<counts.count;i++)
{
temp = counts[i];
if (max<temp)
max = temp;
counter = i;
}
[new_names addObject: [names objectAtIndex:counter]];
[new_numbers addObject: max];
[numbers removeObjectAtIndex: counter];
[names removeObjectAtIndex:counter];
Try something like this. It should work if you do it this way.
Important! do not remove elements in for from array that you count for the for length.
Your problem is in your algorithm design, if you step through it a line at a time in the debugger you should see what it does and where it goes wrong.
We're not here to write you code, but let's see if we can go through one step of an algorithm to help you one your way:
Useful fact: If you lookup a key in a dictionary and that key does not exist the return value will be nil.
From this: you can use a dictionary to keep track of the names you have seen paired with the highest score so far. You obtain a name,score pair, lookup the name in the dictionary - if you get nil its a new name with a new high score. If it's not nil its the currently known high score, so you can compare and update.
That's a rough algorithm, let's try it. Before we start rather than using literal strings for keys everywhere let's define some constants. This has the advantage that we won't mistype the strings, the compiler will spot if we mistype the constant names. These can be defined at the file level or within a method:
const NSString *kName = #"Name";
const NSString *kCount = #"Count";
Now to the code, in a method somewhere, we'll need a dictionary:
NSMutableDictionary *highScores = [NSMutableDictionary new]; // a single dictionary rather than your two arrays
Now start your loop as before:
for (NSDictionary *dict in bigDict) // same loop as your code
{
and extract the two values as before:
NSString *nameString = dict[kName]; // same as your code, but using modern syntax
NSInteger countInt = [dict[kCount] integerValue]; // condense two lines of your code into one
Now we can lookup the name in our dictionary:
NSNumber *currentScore = highScores[nameString]; // get current high score for user, if any
If the name exists as a key this will return the current associated value - the score in this case, if there is no matching key this will return nil. We can test for this in a single if:
if (currentScore == nil // not seen user before, no high score
|| currentScore.integerValue < countInt) // seen user, countInt is greater
{
The above condition will evaluate to true if we either need to add the name or update its score. Adding & updating a key/value pair is the same operation, so we just need the line:
highScores[nameString] = #(countInt); // add or update score for user
and a couple of braces to terminate the if and for:
}
}
Let's see what we have:
NSLog(#"Output: %#", highScores);
This outputs:
Output: {
Jen = 6;
Jerry = 2;
Marge = 3;
Mark = 10;
}
Which is a step in the right direction. (Note: the dictionary is not sorted, NSLog just displays the keys in sorted order.)
Make sure you understand why that works, copy the code and test it. Then try to design the next phase of the algorithm.
If you get stuck you can ask a new question showing the algorithm and code you've developed and someone will probably help. If you do this you should include a link to this question so people can see the history (and know you're not trying to get an app written for you through multiple questions!)
HTH
Try this.
sortedArray = [yourArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
After sort your array then remove duplicates using following.
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray: sortedArray];
NSArray *arrayWithoutDuplicates = [orderedSet array];

How do i get unique contents from my NSMutableArray?

I have a UITableView and am displaying contents from my NSMutableArray. Following is array format
(
{
Name = "ANS";
VersionNo = 6;
},
{
Name = "O-Hydro";
Version = 6;
},
{
Name = "ANS";
Version = 6;
},
{
Name = "ANTIChorosAnticholinergic";
Version = 6;
}
)
From this I need to display only unique "Name" (like in this I can see 2 "ANS" I need only one).
How can I do this in iOS?
I tried following but its not working
uniqueArray= [[NSMutableSet setWithArray: groupDetails] allObjects];
but in this way I can do only for NSArray not NSMutableArray.
Pls help me
You can use following line of code to convert your NSArray to NSMutableArray,
NSArray *uniqueArray= [[NSMutableSet setWithArray:groupDetails] allObjects];
NSMutableArray *myMutableArray = [[NSMutableArray alloc] initWithArray:uniqueArray];
You could simply add mutableCopy.
But wait, before you do it. Arrays and sets have two differences:
Arrays can contain duplicates, sets cannot.
Arrays are ordered, sets are not.
So doing what you are doing, you lose the duplicates (intentionally), but the order, too (probably not intentionally).
I do not know, whether this is important for you, but for other readers it might be. So it is the better approach to do that with NSOrderedSet instead of NSSet:
NSOrderedSet *uniqueList = [NSOrderedSet orderedSetWithArray:array];
In many cases an ordered set is exactly what you want. (Probably it has been from the very beginning and the usage of NSArray was wrong. But sometimes you get an array.) If you really want an array at the end of the day, you can reconvert it:
array = [uniqueList.array mutableCopy];
If you just want an array of unique name values, you can use #distinctUnionOfObjects with valueForKeyPath -
NSArray *uniqueArray=[groupDetails valueForKeyPath:#"#distinctUnionOfObjects.name"];
But if you want the array to contain the dictionaries that correspond to the unique names then you need to do a little more work -
NSMutableArray *uniqueArray=[NSMutableArray new];
NSMutableSet *nameSet=[NSMutableSet new];
for (NSDictionary *dict in groupDetails) {
NSString *name=dict[#"name"];
if (![nameSet containsObject:name]) {
[uniqueArray addObject:dict];
[nameSet addObject:name];
}
}

Removing Objects from NSMutableDictionary

I have a NSDictionary named tableData that contains keys #"A" - #"Z" and for every key the value contains an array with Patient objects.
I then create a NSMutableDictionary named filteredPatients and initialize it with the tableData dictionary.
In this example I try to remove all the Patient objects from the array where the key is #"A"
#property (nonatomic, strong) NSDictionary *tableData;
#property (nonatomic, strong) NSMutableDictionary *filteredPatients;
self.filteredPatients = [[NSMutableDictionary alloc]initWithDictionary:self.tableData];
NSMutableArray *patientArray = [self.filteredPatients objectForKey:#"A"];
int count = patientArray.count - 1;
for (int i = count; i >= 0; i--)
{
[patientArray removeObjectAtIndex:i];
}
In this case, the Patient Objects are getting removed correctly from filteredPatients array with key #"A", but the problem lay in the fact that the same Patient Objects are getting removed from tableData array with key #"A". My question is why are the Patient Objects being removed from the tableData dictionary as well? How do I prevent this?
I must be doing something wrong, but this puzzles me because if I try to simply remove the the key #"A" from filteredPatients, it works perfectly fine and only removes the key from the dictionary I want.
[self.filteredPatients removeObjectForKey:#"A"];
I think your problem is you're expecting deep copy behaviour but you haven't asked for a deep copy. The dictionaries are different objects, but the value for any given key in both dictionaries points to the same mutable array. You could use initWithDictionary:self.tableData copyItems:YES instead, but you're going to need to think through what gets copied and what doesn't. I don't have enough information to help you with that.

Add array to second column of another array

I'm new at Objective C. I have a list of questions, and each question has multiple answers, so I need to have an array which contains another array with a question number, then an array of answers.
I have an NSMutableArray which I'm instantiating it with this line:
_randomQuestionNumberArray = [[NSMutableArray alloc] initWithCapacity:2];
I fill this array with numbers:
for (int i = 0; i < numberOfQuestions; i++) {
NSNumber* xWrapped = [NSNumber numberWithInt:i];
[_randomQuestionNumberArray addObject:xWrapped];
}
Then I have another NSMutableArray which is just a regular array of numbers. I want to add this array to the second column of _randomQuestionNumberArray at a specific row. I'm using this code for this but it doesn't seem to work properly.
[_randomQuestionNumberArray insertObject:_tempAnswers atIndex:position];
Can anyone offer solution to this? Thank you!
Don't use an array of arrays. It will work initially, but it isn't flexible or clear (so hard to maintain).
Instead, use an array of dictionaries, where each dictionary has a number of keys:
_randomQuestionNumberArray = [NSMutableArray array];
for (int i = 0; i < numberOfQuestions; i++) {
[_randomQuestionNumberArray addObject:[#{ #"questionNumber" : #(i) } mutableCopy]];
}
then, when you have your answers:
NSMutableDictionary *questionDict = [_randomQuestionNumberArray objectAtIndex:...];
[questionDict setObject:_tempAnswers forKey:#"answers"];
and now it's obvious what each piece of information is for.
Note: the index of each item in the array could work as the question number if you wanted it to...
Try using this:
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
in place of anObject put your array.

How to check if a NSArray has the values of another array

I have my array unique that is my main array and my array kind. I need to check that only 1 value of kind is present in the array unique. Then if there is more than 1 value of the array kind in unique I need to unset all values but the first one used in the array.
The further i got to achieve this is with the following code but I can not store the indexpath of the found object to do a later comparison. xcode says "bad receiver type nsinteger"
could anyone help me to achieve this?
kind = #[#"#Routine",#"#Exercise",#"#Username"];
NSMutableArray *uniqueKind = [NSMutableArray array];
for (NSString* obj in kind) {
if ( [unique containsObject:obj] ) {
NSInteger i = [unique indexOfObject:obj];
[uniqueKind addObject: [i intValue]];
}
}
An NSInteger is like an int, so you can't send it a message ([i intValue]). Also, you can't add an NSInteger to an array without making it an NSNumber or some other object type. You can do it like this:
NSInteger i = [unique indexOfObject:obj];
[uniqueKind addObject: [NSNumber numberWithInteger:i]];
Also (without understanding what you're doing) you might want to use an NSSet instead of an array. And you can combine a couple of calls:
NSUInteger i = [unique indexOfObject:obj];
if ( i != NSNotFound ) {
[uniqueKind addObject:[NSNumber numberWithInteger:i]];
}
I'm not sure if it would solve your problem, but have you considered using sets (or mutable variation) instead of arrays? They ensure uniqueness and they allow you to check for intersection/containment. See the NSSet class reference.
You have to add objects to the NSMutableArray, not the actual intValue. Try converting teh integer to a NSNumber first.
[uniqueKind addObject: [NSNumber numberWithInt:i]];
instead.
(edited )

Resources