views:

36

answers:

1

I have high scores (name, score, time) stored in a single file and I divide them into separate arrays once it reads them, only problem is i cant figure out a way to sort all three by score and time from least to greatest and still keep the correct order of values. For example:

Name score time
---------------
nathan 123 01:12
bob    321 55:32
frank  222 44:44

turns to:

bob    123 01:12
frank  222 44:44
nathan 321 55:32

Any ideas?

+5  A: 

Encapsulate the data into a single object (HighScore) that has three properties: name, time, and score. Then store them in a single array and sort the single array.

Welcome to object-oriented programming.

Dave DeLong
how would i do that, by creating an abject?
nathanjosiah
Yes, you'll need to create a new class.
Dave DeLong
how would i sort them after creating an object?
nathanjosiah
Using the sorting methods on `NSArray`.... http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/20000137-SW12 and also http://stackoverflow.com/questions/805547
Dave DeLong
nathanjosiah: You'll need to implement the `compare:` method in the model object.
Peter Hosey
how would i do that, i am very new to sorting arrays, i am not semi new to objective c
nathanjosiah
@Peter - not necessarily. He can use an `NSSortDescriptor` to sort the array. That'll use the `compare:` methods already implemented on `NSDate`, `NSString`, etc. Implementing `compare:` is really only necessary if you need to do non-simple comparison (ie, compare multiple properties at once).
Dave DeLong