0

Hello frnds actually i have a array of arrays as given below:-`

parrent array{
    PrArr =       

(

            {
                dt = "01-Apr-2012 11:15 PM\n";
                dur = "15\n\t\t";
                name = "Tez Special\n\t\t";
            },
                        {
                dt = "01-Apr-2012 11:30 PM\n\n";
                dur = "30\n\t\t";
                name = "Tez Tare\n\t\t";
            }
        );
        sid = "530\n";
    },

        {
        PrArr =         (

            {
                dt = "01-Apr-2012 11:20 PM\n";
                dur = "20\n\t\t";
                name = "Shiv Yog - Acharya Ishan Shivanandji\n\t\t";
            },
                        {
                dt = "01-Apr-2012 11:40 PM\n\n\n\t";
                dur = "20\n\t\t";
                name = "Param Pujya Swami HariChaitanya Puriji Maharaj\n\t\t";
            };
            sid = "560\n";
    },

} i want to fetch data according to sid in a array.

Thanx in advance

5
  • Interesting question but little unclear. Which is your parent array? Is 'PrArr' parent array's elements(which are in turn arrays- as ur que implies)? have u used NSDictionay to store dt,dur,name as keys.? Commented Mar 29, 2012 at 6:17
  • PrArr is not parent array ...yes i used nsdictionary .. Commented Mar 29, 2012 at 6:30
  • yes thats only..my guess was right;PrArr is subarray.. Ok, so what logic have you used to store the data in PrArr. I mean it has data inside 2 curly braces & 'sid'. Extract this sid & sort according to requirements & then store in it array. Look for NSSortDescriptor. It will help you. Commented Mar 29, 2012 at 6:49
  • thanx hpiOS Coder .... i m going to check NSSortDescriptor Commented Mar 29, 2012 at 6:56
  • check my answer it help you to quickly sort as per you needs. Dont forget that sort-descriptor just specifies way of sorting. it dont sorts. Sorting is done by Array. Commented Mar 29, 2012 at 7:12

2 Answers 2

0

If you are trying to do find the dictionary where the sid is equal a something, then you will need to use NSPredicate.

From apple documentation:

Predicates provide a general means of specifying queries in Cocoa. The predicate system is capable of handling a large number of domains, not just—for example—Core Data or Spotlight. This document describes predicates in general, their use, their syntax, and their limitations.

With NSPredicate you can find all sid that correspond to your search.

I will give you an example. Based on the array you posted here, you want to return the entry that have the sid equals to @"530\n", what you will need to do is:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"sid LIKE %@",@"530\n"];
NSDictionary* entry = [[parentArray filteredArrayUsingPredicate:predicate] objectAtIndex:0];

But if you are trying to sort your array based on the sid, you can use the NSSortDescriptor, that will return your array sorted based in the sid.

From apple documentation:

A sort descriptor describes a comparison used to sort a collection of objects. You create an instance of NSSortDescriptor that specifies the property key to be sorted, and whether the comparison should be in ascending, or descending order. A sort descriptor can also specify a method to use when comparing the property key values, rather than the default of compare:.

It is important to remember that NSSortDescriptor does not sort objects. It provides the description of how to sort objects. The actual sorting is done by other classes, often NSArray or NSMutableArray.

One example for you

NSSortDescriptor *sidDescriptor= [[NSSortDescriptor alloc] initWithKey:@"sid" ascending:YES];
NSArray* sortDescriptors = [NSArray arrayWithObject:sidDescriptor];    
NSArray* sortedArray = [parentArray sortedArrayUsingDescriptors:sortDescriptors];
Sign up to request clarification or add additional context in comments.

Comments

0

I am just tentatively scribbling it, Check if something like this helps you.

NSSortDescriptor *sidDescr = [[NSSortDescriptor alloc] initWithKey:@"sid" ascending:YES];
sortDescriptors =  [NSArray arrayByAddingObject:sidDescr];    
NSArray * sortedArr = [*yourparentarray* sortedArrayUsingDescriptors:sortDescriptors];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.