user635064
user635064

Reputation: 6247

Searching through array of dictionaries with multiple criteria

I have alot of NSDictioanries in an NSArray. Since there are about 1300 of them, and I need to search through them based of the following criteria:

1. Name
2. DOB
3. Type
4. Grade
5. Condition
6. PUP 
7. Unit Number

Here's an example of such a dictionary:

<Unit>
<UnitNumber>20110501100507134</UnitNumber>
<Name>01'' 2ply Mat</Name>
<DOB>3/24/2011 12:00:00 AM</DOB>
<Type>2ply Mat</Type>
<Grade>Cull</Grade>
<Condition />
<Depth>01</Depth>
<Width>01</Width>
<Length>01</Length>
<PUP>Cable</PUP>
<Finishing />
</Unit>

Right now I am thinking about for looping through the array, search for first criterion, then for loop again through the filtered array and search for 2nd criterion and so on.... But that's 7 for loops, through at-least 1300 dictionaries.

What are my alternatives? Thanks.

UPDATE: Ok so for each of the criteria i listed above, I am going to have text field, and user will specify the criteria there and then press the search button...(if that makes things more clear)

Also, this is for iPhone/iPad

Upvotes: 3

Views: 1258

Answers (3)

Robin
Robin

Reputation: 10011

Well there is an easy way to do it, you can use NSPredicate to search for your array based on the criteria you have mentioned above.

Also as a bonus, If you want to display that array in sorted manner you should use NSSortDiscriptor.

Upvotes: 7

Rayfleck
Rayfleck

Reputation: 12106

While @Robin's answer is spot on, you might also consider using some of the built-in database support, like Core Data.

Upvotes: 1

Chance Hudson
Chance Hudson

Reputation: 2859

Ok, searching through large amounts of data is always going to be tough, especially on an iPhone/iPod. In this case, what i think you should do is only allow the user to search based on one of those criteria, and simply search through individual arrays containing that data. An example of one of those arrays could be

array

matt

bob

mrlol

/array

Then use the index number from that array and extract the dictionary in question. This may not have been the most clear explanation, please comment if you have questions.

Edit: To the previous answer, i don't think he was asking about how to compare data, i think he wanted a way to search through data, so i don't think NSPredicate was what he was looking for.

Upvotes: 0

Related Questions