user3780799
user3780799

Reputation: 51

Sorting big array

i have an array with ~500k objects, and when begin sorting, my app is crashes. _objects is NSMutableArray. Advice me some methods how to make fast sorting of big array.

  - (void)sort1 {
        [_objects sortUsingComparator:^NSComparisonResult(id a, id b) {
            NSNumber *first = [(Obj *)a name];
            NSNumber *second = [(Obj *)b name];
            return [second compare:first];
        }];

        [self.tableView reloadData];
    }

Upvotes: 0

Views: 77

Answers (2)

user2338215
user2338215

Reputation: 831

With that much data, you might consider using a different approach. After all, no one is likely to scroll through a tableview containing ~500k items. It might make more sense to store the data in an SQLITE database, and then query the DB for the items of interest and sort only those items.

Upvotes: 2

Scott Hunter
Scott Hunter

Reputation: 49813

Some ideas:

  • Sort smaller portions of the array in sequence, and then merge them together (which could be done in multiple stages).

  • Taking that idea further, you could use one or more files to store intermediate arrays, and thus reduce the amount of memory needed (though that would most likely slow things down, its better than crashing)

Upvotes: 1

Related Questions