Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HarryWorksInc

macrumors regular
Original poster
Feb 21, 2010
179
0
Is there a way to organize an NSArray by number? I have been looking this up for a while but can't find anything.
 
I don't understand the question. An NSArray is already indexed by "number". Each item in the array is at a specific index.

Do you mean "Can I sort an NSArray of NSNumbers into increasing numeric order of the NSNumber values?"
 
Sorry, let me clarify. I mean every object in the array is a numerical value, is there a way to sort all of the objects in the array by their numerical value?
 
Did you actually open the documentation? There are many sort operations on NSArray. I'd recommend:

sortedArrayUsingSelector:

and a suitable selector. If all your objects are NSNumbers then

compare:

If you have custom objects you'll have to write your own comparison method and use that selector.
 
sorry I was looking around those I just didn't find that I guess but thanks it works great.

One last question though I have dates stored in an array for another view and am trying to sort that by date I have looked over the links that you gave me but I could not see anything for that.
 
You need to look at whatever class you are using to store your dates and find a compare: or similar selector. If you can't find one you'll have to write one. It's not my job to do your research for you. Using the documentation is a core programming skill: learn it.
 
If you have an array of dates you can sort them the same way as an array of numbers. NSDate also has a compare: method.

All of the Foundation classes for which it makes sense to compare them or sort them have a compare: method and an array of them can be sorted by built-in methods.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.