iOS Search Results Order - iphone

I have a UISearchBar that searches a table of data. When a search is typed in, the results are displayed in an order like so:
Starts typing 'ch...'
1. Ache
2. Cherries
3. Choice
It makes more sense to me (and for my app) that 'Cherries' and 'Choice' would be at the top of the results, because 'ch' and not 'ach' was typed in. Is this something that can be changed programatically, or is it just the way iOS searches work?

You have to give the search option as NSAnchoredSearch
NSRange searchRange = [sortedString rangeOfString:searchText options:NSAnchoredSearch];
Some of the search method listed below
NSCaseInsensitiveSearch
NSLiteralSearch
NSBackwardsSearch
NSAnchoredSearch
NSNumericSearch
NSDiacriticInsensitiveSearch
NSWidthInsensitiveSearch
NSForcedOrderingSearch
NSRegularExpressionSearch
Eg:
- (void)search {
NSString *searchText = [searchBar.text lowercaseString];
for (int index = 0; index < [availableCollectionArray count]; index++) {
NSArray *tempArray = [availableCollectionArray objectAtIndex:index];
for (int tempIndex = 0; tempIndex < [tempArray count] ; tempIndex++) {
NSString *sortedString = [tempArray objectAtIndex:tempIndex];
NSRange searchRange = [sortedString rangeOfString:searchText options:NSAnchoredSearch];
if (searchRange.length > 0)
{
[sortedArray addObject: sortedString]; //add the string which starts from searchBar.text
}
}
}
}

Related

Search String into NSArray based on charcters order?

My Problem Scenario is like this. I have an NSMutableArray ( Every Object is Nsstring). I have a UItextField ( as Client said) for Search.
I want know how to Search String into NSMutableArray like this
if I type A into textfield only those Content come from NSMutableArray which start From A.
if I type AB into TextField only those Content Comes from NSMutableArray which is started from AB..
....
I am Trying NSRange Concept I like share Mycode
~
for (int i=0; i<[[localTotalArrayForAwailable objectForKey:#"PUNCH"] count]; i++)
{
NSString *drinkNamePuch= [[[localTotalArrayForAwailable objectForKey:#"PUNCH"] objectAtIndex:i] drinkNames];
NSRange titleResultsRange = [drinkNamePuch rangeOfString:searchText options:( NSCaseInsensitiveSearch)];
if (titleResultsRange.length>0)
{
[searchArraypuch addObject:[[localTotalArrayForAwailable objectForKey:#"PUNCH"] objectAtIndex:i]];
[copyListOfItems setValue:searchArraypuch forKey:#"PUNCH"];
}
}
~
Based on this code search not working proper as i need.
Thanks
If you're trying to find all of the strings that match your searchText from the beginning, then you should check:
if ( titleresultsRange.location == 0 )
Other than that, I am not sure what is "not working proper", you need to provide a better explanation of what your expected results are, and what your actual results are.
Do this;
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"SELF BEGINSWITH[cd] %#", searchText];
NSArray* filteredStrings = [[localTotalArrayForAwailable objectForKey:#"PUNCH"] filteredArrayUsingPredicate:predicate];
In filteredStrings you got all the strings that begins with searchText.
You might find Predicate Programming Guide helpful.
try this logic....it is working
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"aa",#"bbb",#"bb",#"cc",#"dd",#"ee",#"ff",#"gg",#"hh",#"ii", nil];
NSMutableArray *arrNew = [[NSMutableArray alloc]init];
NSString *strSearch = #"cccc";
int k = strSearch.length;
for (int i=0; i<[arr count]; i++) {
for (int j=0; j<k; j++) {
if (k<=[[arr objectAtIndex:i] length]) {
if ([strSearch characterAtIndex:j] != [[arr objectAtIndex:i]characterAtIndex:j]) {
break;
}
else if(j == k-1){
[arrNew addObject:[arr objectAtIndex:i]];
}
}
}
}
NSLog(#"%#",[arrNew description]);
You can use these methods, which are provided by NSArray/NSMutableArray:
In NSArray see section "Finding Objects in an Array" for filtering methods starting with "indexesOfObjects...", e.g. indexesOfObjectsPassingTest:
In NSArray see section "Deriving New Arrays" for the method filteredArrayUsingPredicate:
In NSMutableArray there is a method filterUsingPredicate:
For narrowing the results you can continue applying the filtering consecutively to the filtered arrays or index sets.
Example with indexesOfObjectsPassingTest: using a block:
NSArray *strings = [NSArray arrayWithObjects:#"A", #"a", #"aB", #"AbC", #"Bag", #"Babc", #"baCK", #"", #"dba", nil];
NSString *searchString = #"Ab";
BOOL (^startsWithPredicate)(id, NSUInteger, BOOL*) = ^BOOL (id obj, NSUInteger idx, BOOL *stop) {
NSString *string = (NSString *) obj;
NSRange range = [string rangeOfString:searchString options:NSCaseInsensitiveSearch];
return (range.location == 0);
};
NSIndexSet *indexSet = [strings indexesOfObjectsPassingTest:startsWithPredicate];
NSLog(#"Strings found: %#", [strings objectsAtIndexes:indexSet]);
Output:
Strings found: (
aB,
AbC
)

range of substring in a string

I have an array containing object let say { my saxophone, take, everywhere}. Secondly i have a NSString: 'take my saxophone everywhere' then how can I able to match object of array with string and get index from NSString. I need index of substring from NSString by comparing with objects of array. So that i can sort the array correctly. So far I have done
NSRange range = [feedBackAnswer rangeOfString:#"everywhere"];
if ( range.length > 0 )
{
NSLog(#"range.location..%d",range.location);
}
else
{
NSLog(#"...%d",-1);
}
But I could not find the index of substring.
NSMutableArray * data=[[NSMutableArray alloc]initWithObjects:#"my saxophone",#"take",#"everwhere", nil];
NSString * mat=#"take my saxophone everywhere";
for (int i=0; i<[data count]; i++) {
NSRange range = [mat rangeOfString:[data objectAtIndex:i]];
if (range.length > 0)
NSLog(#"Range is: %#", NSStringFromRange(range));
else
NSLog(#"Failed");
}
[data release];
& check the spelling in array (everwhere)
Range of sub string in a string is ...
0: U
1: n
2: i
3: v
4: e
5: r
6: s
7: i
8: t
9: y
Try this code
NSMutableArray *array1 = [NSMutableArray arrayWithObjects:#"my",#"saxophone",#"take",#"everywhere", nil];
NSString *str1 = #"take my saxophone everywhere";
NSArray *array = [str1 componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#" objects : %#",array);
for (int i=0; i<[array count]; i++) {
for (int j=0; j<[array count]; j++) {
if ([[array objectAtIndex:i] isEqualToString:[array1 objectAtIndex:j]]) {
NSLog(#"String :%# no : %d ",[array objectAtIndex:i],j);
}
}
}

Better way to handle arrays in Objective-C?

Please let me preface this question with an apology. I am very new to Objective C. Unfortunately, I have a very tight timeline on a project for work and need to get up to speed as fast as possible.
The code below works. I am just wondering if there is a better way to handle this... Maybe something more Cocoa-ish. The entire purpose of the function is to get an ordered list of positions in a string that have a certain value.
I fell back on a standard array for comfort reasons. The NSMutableString that I initially declare is only for testing purposes. Being a complete noob to this language and still wrapping my head around the way Objective C (and C++ I guess) handles variables and pointers, any and all hints, tips, pointers would be appreciated.
NSMutableString *mut = [[NSMutableString alloc] initWithString:#"This is a test of the emergency broadcast system. This is only a test."];
NSMutableArray *tList = [NSMutableArray arrayWithArray:[mut componentsSeparatedByString:#" "]];
int dump[(tList.count-1)];
int dpCount=0;
NSUInteger length = [mut length];
NSRange myRange = NSMakeRange(0, length);
while (myRange.location != NSNotFound) {
myRange = [mut rangeOfString:#" " options: 0 range:myRange];
if (myRange.location != NSNotFound) {
dump[dpCount]=myRange.location;
++dpCount;
myRange = NSMakeRange((myRange.location+myRange.length), (length - (myRange.location+myRange.length)));
}
}
for (int i=0; i < dpCount; ++i) {
//Going to do something with these values in the future... they MUST be in order.
NSLog(#"Dumping positions in order: %i",dump[i]);
}
text2.text = mut;
[mut release];
Thanks again for any replies.
There is not great way to do what you are trying to do. Here is one way:
// locations will be an NSArray of NSNumbers --
// each NSNumber containing one location of the substring):
NSMutableArray *locations = [NSMutableArray new];
NSRange searchRange = NSMakeRange(0,string.length);
NSRange foundRange;
while (searchRange.location < string.length) {
searchRange.length = string.length-searchRange.location;
foundRange = [string rangeOfString:substring options:nil range:searchRange];
if(foundRange.location == NSNotFound) break;
[locations addObject:[NSNumber numberWithInt:searchRange.location]];
searchRange.location = foundRange.location+foundRange.length;
}
This might be a little more streamlined (and faster in terms of execution time, if that matters):
const char *p = "This is a test of the emergency broadcast system. This is only a test.";
NSMutableArray *positions = [NSMutableArray array];
for (int i = 0; *p; p++, i++)
{
if (*p == ' ') {
[positions addObject:[NSNumber numberWithInt:i]];
}
}
for (int j = 0; j < [positions count]; j++) {
NSLog(#"position %d: %#", j + 1, [positions objectAtIndex:j]);
}

How to sort an array with alphanumeric values?

I have an array which contains strings like frame_10#3x.png , frame_5#3x.png,frame_19#3x.png etc.
So I want to sort this array according to the number after the underscore i.e. the correct sequence will be frame_5#3x.png,frame_10#3x.png,frame_19#3x.png.
I tried to use the following method but no result:
NSInteger firstNumSort(id str1, id str2, void *context) {
int num1 = [str1 integerValue];
int num2 = [str2 integerValue];
if (num1 < num2)
return NSOrderedAscending;
else if (num1 > num2)
return NSOrderedDescending;
return NSOrderedSame;
}
Please suggest how to do this sorting for array.
NSArray *sry_img = [[NSArray alloc] initWithObjects:#"frame_18#3x.png",#"frame_17#3x.png",#"frame_1222#3x.png",#"frame_10#3x.png",#"frame_3#3x.png",#"frame_4#3x.png",#"frame_4#3x.png",#"frame_1#3x.png",#"frame_4#3x.png",#"frame_4#3x.png",nil];
NSArray *sortedStrings = [sry_img sortedArrayUsingSelector:#selector(localizedStandardCompare:)];
NSLog(#"%#",sortedStrings);
Enjy .......
But
localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
you want to do something like:
NSArray *components1 = [str1 componentsSeparatedByString:#"_"];
NSArray *components2 = [str2 componentsSeparatedByString:#"_"];
NSString *number1String = [components1 objectAtIndex:([components1 count] - 1])];
NSString *number2String = [components2 objectAtIndex:([components2 count] - 1])];
return [number1String compare:number2String];
I am not sure if my solution is the best possible approach but it can solve your problem for the time being :) .
1) First I have written a function to get the numbers before # character in your string and then I implemented simple SELECTION SORT algo to sort the array using this functions.
- (NSString*)getSubStringForString:(NSString*)value {
// First we will cut the frame_ string
NSMutableString *trimmedString = [NSMutableString stringWithString:[value substringWithRange:NSMakeRange(6, [value length]-6)]];
// New String to contain the numbers
NSMutableString *newString = [[NSMutableString alloc] init];
for (int i = 0; i < [trimmedString length] ; i++) {
NSString *singleChar = [trimmedString substringWithRange:NSMakeRange(i, 1)];
if (![singleChar isEqualToString:#"#"]) {
[newString appendString:singleChar];
} else {
break;
}
}
return newString;
}
This is the selection Implementation of the algo for sorting. The main logic is in the for loop. You can copy the code in viewDidLoad method to test.
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"frame_10#3x.png",#"frame_5#3x.png",
#"frame_3#3x.png", #"frame_19#3x.png",
nil];
NSLog(#"Values before Sort: %#", array);
int iPos;
int iMin;
for (iPos = 0; iPos < [array count]; iPos++)
{
iMin = iPos;
for (int i = iPos+1; i < [array count]; i++)
{
if ([[self getSubStringForString:[array objectAtIndex:i]] intValue] >
[[self getSubStringForString:[array objectAtIndex:iMin]] intValue]) {
iMin = i;
}
}
if ( iMin != iPos )
{
NSString *tempValue = [array objectAtIndex:iPos];
[array replaceObjectAtIndex:iPos withObject:[array objectAtIndex:iMin]];
[array replaceObjectAtIndex:iMin withObject:tempValue];
}
}
NSLog(#"Sorted Values: %#", array);
I hope that it can atleast keep you going. :)
You can try this-
NSString *str1 = [[[[str1 componentsSeparatedByString:#"frame_"] objectAtIndex:1] componentsSeparatedByString:#"#3x.png"] objectAtIndex:0];
int num1 = [str1 integerValue];

split NSArray in groups of smaller NSArrays for paging

I have an NSArray of say 100 NSManagedObjects and I need to split that into an NSArray that contains 10 NSArray objects that each hold 10 of theses NSManagedObjects, how would I accomplish that?
I am going to do some paging and this will work well for me.
How are you getting these NSManagedObjects? If you're using an NSFetchRequest, you may want to keep that around and only get 10 results at a time from it.
Here is my code:
[object splitArrayWithArray:arrayWith100Objects rangeNumber:10];
- (NSArray*) splitArrayWithArray:(NSArray*)rawArray rangeNumber:(int)rangeNumber{
int totalCount = rawArray.count;
int currentIndex = 0;
NSMutableArray* splitArray = [NSMutableArray array];
while (currentIndex<totalCount) {
NSRange range = NSMakeRange(currentIndex, MIN(rangeNumber, totalCount-currentIndex));
NSArray* subArray = [rawArray subarrayWithRange:range];
[splitArray addObject:subArray];
currentIndex +=rangeNumber;
}
return splitArray;
}
Something along these lines should work instead of breaking it into separate arrays
//make a new range for each page
NSRange myRange = NSMakeRange(10, 10);
NSArray * myArray = [NSArray array];
//...pass both into a function
for (int i = myRange.location; i < myRange.location + myRange.length; i++) {
//stuff with the array elements
[[myArray objectAtIndex:i] doSomething];
}
It's no one-liner, but this will split an array into pages:
NSArray *arrayToSplit = ...
int pageSize = 50;
NSMutableArray *arrayOfPages = [NSMutableArray new];
NSRange range = NSMakeRange(0, pageSize);
while (range.location < arrayToSplit.count) {
if (range.location + range.length >= arrayToSplit.count)
range.length = arrayToSplit.count - range.location;
[arrayOfPages addObject:[arrayToSplit objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range]]];
range.location += range.length;
}