This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to sort a NSArray alphabetically?
Hi all,
I have an array which consist of companies name. I want to sort array alphabetically so that companies name come in alphabetic order.
Please suggest me how to do this.
Thanks in advance
[myArray sortUsingSelector:#selector(caseInsensitiveCompare:)];
NSArray *sortedStrings = [strings sortedArrayUsingSelector:#selector(compare:)];
If the array consists of NSStrings:
[companies sortUsingSelector:#selector(compare:)];
Related
This question already has answers here:
How to check if an element is in an array
(18 answers)
Closed 7 years ago.
Swift 2 - so, I have an array and a uitextfield that a user inputs a string, I want to check whether the textfield.text is equal to ANY of the values in the array, can I do this with one line of code rather than lots of if's and else if's?!?
This is a generic code that will do what you are looking for. The if statement checks to see if a given value is equal to something that is located in the array. Simply replace the arr.contains() with the output you have given for your UITextfield.text Try to do a little research before you post. I can see that you are new here, so here is a little bit of help.
var arr = [1,2,3,4]
if arr.contains(3) {
//do something
}
This question already has answers here:
case insensitive find in mongodb for usernames in php
(2 answers)
Closed 8 years ago.
I am doing with function in mongoDB, now it's have problem with case-insensitive data. this is my code in function
$where = array(TblFact::Fou_Name => array('$regex' =>$SearchNameFactory));
this code when data in uppercase and i search by lowercase is return null. So anyone can help me to find solution for case-insensitive query ?
I am looking to see your replay soon. Thanks ...
Thanks everyone for help me , now my problem have been resolve by
$where = array(TblFact::Fou_Name => new MongoRegex("{$SearchNameFactory}/i"));
hope it can help to anyone who meet problem like me
thanks
This question already has answers here:
iPhone/iOS JSON parsing tutorial [closed]
(6 answers)
Closed 9 years ago.
I have a string that looks like this:
"[{"ImageTagID":78,"Xpixel":408,"Ypixel":69,"Xpercent":17,"Ypercent":68,"ImageID":45617}]"
How do i break it up to pieces?
Thanks,
This is a dictionary. There are keys and values in a dictionary. If you need a particular value, you must get it using the key.
In iOS it is done this way,
int tagID = [yourdictionary objectForKey:#"ImageTagID"];
Hope this helps you start. If you have more questions, do ask me.
Splitting a string:
Example: NSArray *stringArray = [string componentsSeparatedByString: #","];
I've been trying to see if there is any way I can improve on the performance of the following Predicate:
[NSPredicate predicateWithFormat:#"A=%# and SUBQUERY($self.words,$k,$k.keyword IN %#).#count==%d",
<value for A>,
keywordList,
[keywordList count]];
What I'm trying to do is return all the records of A that have keywords that are ALL contained in the supplied array (keywordList). I have a small database of about 2000 records. However, the keywords for each Entity ranges from 40-300 keywords. The keywords are represented as a to-Many relationship from A to an entity called Keywords. The above query works but takes about 7 seconds to run on my iPhone4. I want to see what I can do to shrink that down to a sub-second response.
Thanks,
Michael
This doesn't quite solve the problem as the results that come back from intersection of the fetchedObjs is not correct. It doesn't guarantee that all the 'A' objects contain all the supplied keywords. In fact, what comes back is an empty list and the entire process actually takes longer.
Something must be wrong with my model or the previous answer. I'm not getting the results I'm expecting. One alternative to returning a list of 'A' would be to return a list of ManagedObjectIDs of 'A'. [mo valueForKey: (NSString *)] returns the object of the relationship.
Note: I originally posted this question anonymously thinking I was logged in, so i can't seem to comment on anybody's answer.
I think you are approaching the problem backwards. If you have the keywords, you should search for the Keyword objects and then walk their A relationships to find the related A objects. Then check for overlap in the relationship sets.
So something like:
NSFetchRequest *fetch=//...set up fetch
[fetch setEntity:Keyword_entity];
NSPredicate *p=[NSPredicate predicateWithFormat:#"keyword IN %#",keywords];
[fetch setPredicate:p];
NSArray *fetchedObj=//... execute fetch
NSMutableSet *inCommon=[NSMutableSet setWithCapacity:[fetchedObjs count]];
for (NSManagedObject *mo in fetchedObjs){
if ([inCommon count]==0){
[inCommon addObjects:[mo valueForKey:#"aRelationshipName"]];
}else{
[inCommon intersectSet:[mo valueForKey:#"aRelationshipName"]];
}
}
... inCommon would then contain a set of all unique A objects that shared all the keywords.
Update:
From OP author:
This doesn't quite solve the problem
as the results that come back from
intersection of the fetchedObjs is not
correct. It doesn't guarantee that all
the 'A' objects contain all the
supplied keywords.
Okay, lets take another tack. Assuming you have a data model that looks something like this:
A {
keywords<<-->>Keyword.as
}
Keyword{
keyword:string
as<<-->>A.keywords
}
Then something like this should work:
NSFetchRequest *keyWordFetch=//...set up fetch
[keyWordFetch setEntity:Keyword_entity];
NSPredicate *p=[NSPredicate predicateWithFormat:#"keyword IN %#",keywords];
[keyWordFetch setPredicate:p];
NSArray *fetchedKeywords=//... execute keyWordFetch
NSFetchRequest *entityAFetch=//...set up fetch
[entityAFetch setEntity:A_entity];
NSPredicate *pred=[NSPredicate predicateWithFormat:#"ALL keywords IN %#", fetchedKeywords);
[entityAFetch setPredicate:pred];
NSArray *fetchedAs=//... execute fetch
i have two strings initialised with using nsstring *name1,*name2...
how to compare these
if([name1 isEqualToString:name2]){
NSLog("These are the same name");
}
The best place for API features like this is the Apple documentation