views:

261

answers:

2

Hello,

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.

+3  A: 

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.

Felix Kling
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
@Amir: So you should be fine with the normal sort functions.
Felix Kling
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
@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.
Felix Kling
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.
Felix Kling
ok i understand , but as far as i know NSArray is for static and NSMutableArray for dynamic. what i try to do is dynamic so i think i will need NSMutableArray . can i use the method "writeToFile:filePath atomically:" that save the Array to file from NSArray also with NSMutableArray ?
Amir
@Amir: Every method that is available for NSArray is also available for NSMutableArray. NSMuatableArray is a **subclass** if NSArray, that means it inherits every method from NSArray.
Felix Kling
@Felix Technically `NSMutableArray` and `NSArray` abstract, and arrays are actually the same type, `NSCFArray`, where mutability is determined by an internal flag. But for simplicity, you're correct. ;)
Dave DeLong
+1 for Felix's teacher patience. @Amir: Ignore Dave's note just above-- it's correct, but more advanced than you need to know right now. (@Dave: no offense.)
quixoto
@Quixoto none taken. :)
Dave DeLong
+1  A: 

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.

Dave DeLong
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
@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.
Dave DeLong
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 bemy_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
Amir