• 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: sorting large tables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sorting large tables


  • Subject: Re: sorting large tables
  • From: Daniel Child <email@hidden>
  • Date: Thu, 13 Mar 2008 23:34:19 -0400

On Mar 13, 2008, at 6:24 PM, Jens Alfke wrote:

You don't necessarily need to sort all of it at once. You just need to find the first few items, to display in your table view. If the user scrolls past those, you need to find more. You can do this by streaming the data from a file, keeping only the number of items you need in memory at once. This sort of task is typically called "external sorting", and Knuth and most other algorithm texts talk about it.
Hmm, I'm not displaying in a table. I am simply trying to sort a large file for later use (so that later it can be brought up bits at a time in a meaningful order). But I get the point, I may need to try doing an external sort.

But that said, the sort you're doing shouldn't freeze or crash, not unless you eat up your processes' entire address space or use up so much VM that the system starts to thrash. You'll have to provide more details of what's happening.
It sounds like my program shouldn't be freezing in the first place, since my files are not THAT big. Here are the details.

@interface GenericRecord : NSObject <NSCopying>
{
  NSMutableArray *record;
  ... plus a couple ivars not used for the sort
}
@interface GenericTable : NSObject <NSCoding>
{
  NSMutableArray *records; // the records of data
  ... plus a couple ivars not used for the sort
}

// IN GenericTable implementation
- (void) sortRecordsByField: (int) fieldNum;
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // experimental! initially ran without it...
NSArray *sortedArray;
void *i;
i = (void *) &fieldNum;
sortedArray = [records sortedArrayUsingFunction: sortGenericRecord context: i]; // FROZE AFTER 20 MIN OF EXECUTION HERE
// [self setRecords: [NSArray arrayWithArray: sortedArray]];
[self setRecords: [NSMutableArray arrayWithArray: sortedArray]]; // DID NOT GET THIS FAR....
[pool release];
}


// in GenericRecord implementation
int sortGenericRecord (GenericRecord *rec1, GenericRecord *rec2, void *fieldNum)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int *columnPtr = (int *) fieldNum;
int column = *columnPtr;
NSString *rec1Field = [rec1 fieldAtIndex: column]; // no new alloc, just ptr assignment
NSString *rec2Field = [rec2 fieldAtIndex: column]; // ibid => how can I make smaller footprint?
NSLog(@"REC 1: %@ REC 2: %@\n", rec1Field, rec2Field);
return [rec1Field compare: rec2Field];
[pool release]; // I'm not sure whether the pool helps since no convenience constructors, but tried anyway.
}


An alternative method passes an array via the void pointer so that I can have primary, secondary... n'ary sort fields. Not shown, since I at this point I'm not even past first base. Thanks.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: sorting large tables
      • From: Jens Alfke <email@hidden>
    • Re: sorting large tables
      • From: Nir Soffer <email@hidden>
    • Re: sorting large tables
      • From: Dave Hersey <email@hidden>
References: 
 >Re: sorting large tables (From: Daniel Child <email@hidden>)
 >Re: sorting large tables (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Strange retainCount from NSTimer
  • Next by Date: Can core data save to a remote file?
  • Previous by thread: Re: sorting large tables
  • Next by thread: Re: sorting large tables
  • Index(es):
    • Date
    • Thread