BSD

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Sorting an NSArray

    6 answers - 719 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I have a simple class:
    @interface MyClass : NS
    {
    NSString *name;
    NSString *address;
    }
    - (NSString *)name;
    - (void)setName(NSString *)name;
    - (NSString *)address;
    - (void)setAddress(NSString *)address;
    @end
    And then I populate an NSArray with objects of this type, is it
    possible to then sort the array by either the "name" or "address"
    properties?
    Cheers for any help.
    D.
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com
    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.1 | | 519 bytes | |

    Jun 30, 2005, at 1:16 AM, Duncan Campbell wrote:

    And then I populate an NSArray with objects of this type, is it
    possible to then sort the array by either the "name" or "address"
    properties?

    <
    +>

    mmalc

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.2 | | 764 bytes | |

    Jun 30, 2005, at 1:16 AM, Duncan Campbell wrote:
    And then I populate an NSArray with objects of this type, is it
    possible to then sort the array by either the "name" or "address"
    properties?

    Yes. You can use the -sortedArrayUsingXXX methods of NSArray, or if
    your array is mutable, you can sort it in place with the -
    sortUsingXXX methods of NSMutableArray.

    Example:

    NSArray *sortedArray =
    [oldArray sortedArrayUsingSelector:@selector(name)];

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.3 | | 741 bytes | |

    Jun 30, 2005, at 1:27 AM, Andy Lee wrote:
    Example:

    NSArray *sortedArray =
    [oldArray sortedArrayUsingSelector:@selector(name)];

    , that isn't right. The selector you pass is supposed to be a
    comparison method, which is not what -name is. What you should do is
    write a comparison method in your object class, and pass the selector
    for that method.

    It's probably clearer if you check out the docs mmalcolm pointed to.

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.4 | | 985 bytes | |

    30 jun 2005, at 10.46, Andy Lee wrote:

    , that isn't right. The selector you pass is supposed to be a
    comparison method, which is not what -name is. What you should do
    is write a comparison method in your object class, and pass the
    selector for that method.

    Stop to think if you really need to write a custom comparison method
    - it's probably only necessary if you need to compare non-object
    values or perform a compound comparison between several attributes.
    The question asked in the first message in this thread didn't give
    that impression.

    It's probably clearer if you check out the docs mmalcolm pointed to.

    Agreed!

    j o a r

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.5 | | 1485 bytes | |

    not sure what's going on, but i never saw the email containing "the
    docs mmalcolm pointed out" - can someone forward me the email?

    Cheers.

    30/06/2005, at 6:53 PM, j o a r wrote:

    30 jun 2005, at 10.46, Andy Lee wrote:
    >
    >
    >, that isn't right. The selector you pass is supposed to be a
    >comparison method, which is not what -name is. What you should do
    >is write a comparison method in your object class, and pass the
    >selector for that method.
    >>

    >

    Stop to think if you really need to write a custom comparison
    method - it's probably only necessary if you need to compare non-
    object values or perform a compound comparison between several
    attributes. The question asked in the first message in this thread
    didn't give that impression.
    >
    >
    >It's probably clearer if you check out the docs mmalcolm pointed to.
    >>

    >

    Agreed!

    j o a r
    >
    >
    >


    Duncan Campbell

    http://www.duncanandsarah.com

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.6 | | 1420 bytes | |

    Jun 30, 2005, at 1:53 AM, j o a r wrote:
    30 jun 2005, at 10.46, Andy Lee wrote:
    >, that isn't right. The selector you pass is supposed to be a
    >comparison method, which is not what -name is. What you should do
    >is write a comparison method in your object class, and pass the
    >selector for that method.
    >>

    >

    Stop to think if you really need to write a custom comparison
    method - it's probably only necessary if you need to compare non-
    object values or perform a compound comparison between several
    attributes. The question asked in the first message in this thread
    didn't give that impression.

    Hm, taking a hint, I've discovered NSSortDescriptor, which I can now
    use myself since I'm no longer supporting Jaguar in my app. (I'll
    have to check whether it buys me anything.)

    I won't try to explain NSSortDescriptor at this hour, for fear of
    embarrassing myself -- I'll just repeat the advice to see the docs
    and post again if they're not clear.

    Thanks.

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com

Re: Sorting an NSArray


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "BSD"

EMSDN.COM