• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sort Array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sort Array


  • Subject: Re: Sort Array
  • From: Steve Mills <email@hidden>
  • Date: Sat, 16 Nov 2002 13:48:50 -0600

On Saturday, November 16, 2002, at 05:44 AM, Lorenzo Puleo <email@hidden> wrote:

I have a short list with "Name" and "Surname" fields.
I want to sort this list by "Surname".
How should I build the NSArray?
How should I use the NSArray methods?

One of two ways:

A) Find the insertion position yourself and use [myList insertObject: obj atIndex: i].

B) Just add the object to the end with [myList addObject: obj], then sort it using [myList sortUsingSelector: @selector(mySortRoutine:)].

A is faster (but only if you let the list get way out of sort), B is easier. Actually, A is easy too:

for(i = 0; i < [myList count]; i++) {
MyClass* obj = [myList objectAtIndex: i];

if(newObj->offset < obj->offset) {
[myList insertObject: newObj atIndex: i];
break;
}
}

You could even speed it up by using a binary insertion location search, but this one is fast enough for lists with relatively few elements and fast object comparisons.

Steve Mills
Drummer, Mac geek
http://sjmills5.home.mchsi.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: How to use setNeedsDisplayInRect?
  • Next by Date: Re: Cocoa WebObjects
  • Previous by thread: Re: Sort Array
  • Next by thread: Default project bug?
  • Index(es):
    • Date
    • Thread