1

I am writting program for my iphone and have a qestion.
lets say i have class named my_obj

class my_obj  
{  
  NSString  *name;  
  NSinteger  *id;  
  NSinteger  *foo;  
  NSString   *boo;  
}  

now i allocate 100 objects from type my_obj and insert them to array from type NSArray.
then i want to sort the Array in two different ways. one by the name and the second by
the id.

i want to allocate another two arrays from type NSArray

*arraySortByName     
*arraySortById  

what i need to do if i just want the sorted arrays to be referenced to the original array
so i will get two sorted arrays that point to the original array (that didnt changed!)
i other word i dont want to allocate another 100 objects to each sorted array.

1
  • What language is the class declared in? Is that meant to be C++? Commented May 30, 2010 at 14:03

2 Answers 2

4

You can use the NSArray method sortedArrayUsingFunction:context: (or the other NSArray sort functions) which returns a new array.

From the documentation:

The new array contains references to the receiver’s elements, not copies of them.


If you mean that the sorted arrays should just be pointers to the original array, how is this supposed to work? The order of the elements in the sorted arrays is different.

Or I didn't get your question.

Sign up to request clarification or add additional context in comments.

9 Comments

what i want to do is to print the sorted arrays in few places in my code and i dont want to sort them everey time i want to print the array. so i will keep two sorted array that referenced to the original array and print them whenever i want.
@Amir: So you should be fine with the normal sort functions.
regard the first answer: i cant use NSArray because the array is not from type NSString but my_object. so i have to use the nsmutablearray and there the method sortedArrayUsingFunction:context: will return void. so the answer didnt help me much.
@Amir: you can use NSArray for any class that inherits from NSObject. Also, I am talking about the method sortedArrayUsingFunction:context: (note: sorted), not sortArrayUsingFunction:context:. The first one always returns a new array. The latter is only available in NSMutableArray and sorts in-place.
Edit previous comment: I mean sortUsingFunction:context:. I think you confuse this method with the one I mentioned. NSMutableArray is a subclass of NSArray. It cannot override the parents method and return void.
|
2

This is really easy. Check it:

NSSortDescriptor * sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSSortDescriptor * sortByID = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];

NSArray * sortedByName = [original sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByName]];
NSArray * sortedByID = [original sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByID]];

This will not create copies of your objects. So you will still have only 100 allocated my_obj objects.

3 Comments

I not fully understand what NSSortDescriptor do? what is the sortDescriptorWithKey? lets say that i want to sort by the property name(string) how i initialize the NSSortDescriptor * sortByName object to do this? if i refer to the above code and i assume that the array original have 100 objects of my_obj , does the object sortedByName have reference to the original 100 objects so they sorted alfa betic according to property name(string) from my_obj?
@Amir a sort descriptor is an abstraction of how to sort a collection of objects. Think of it as an "ORDER BY" clause in a SQL query. If you want to sort by an object's name, then the key you use is @"name". To sort by myIntegerProperty, the key is @"myIntegerProperty". Your objects will not be copied. The sorted array will just contain references to your original objects.
Someone can help me with declaration of 100 object from type my_obj? i have xml message which contain information about 100 objects from type my_obj how can i allocate 100 object in advance? in c++ this will be my_obj obj[100]; or my_obj *obj = new obj[100]; what is the syntax in objective c? and how i access to those objects? (in c++: obj[x] = etc..) thanks

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.