Organizing an NSArray by number

Discussion in 'iOS Programming' started by HarryWorksInc, Sep 27, 2010.

  1. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #1
    Is there a way to organize an NSArray by number? I have been looking this up for a while but can't find anything.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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?"
     
  3. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #3
    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?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #5
    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.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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.
     
  8. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010

Share This Page