I'm currently using NSMutableArrays
in my developments to store some data taken from an HTTP Servlet.
Everything is fine since now I have to sort what is in my array.
This is what I do :
NSMutableArray *array = [[NSMutableArray arrayWithObjects:nil] retain];
[array addObject:[NSArray arrayWithObjects: "Label 1", 1, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 2", 4, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 3", 2, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 4", 6, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 5", 0, nil]];
First column contain a Label and 2nd one is a score I want the array to be sorted descending.
Is the way I am storing my data a good one ? Is there a better way to do this than using NSMutableArrays
in NSMutableArray
?
I'm new to iPhone dev, I've seen some code about sorting but didn't feel good with that.
Thanks in advance for your answers !
You don't need to create a custom class for something so trivial, it's a waste of code. You should use an array of
NSDictionary
's (dictionary in ObjC = hash in other languages).Do it like this:
This would be much easier if you were to create a custom object (or at least use an
NSDictionary
) to store the information, instead of using an array.For example:
Once you've populated your
scores
array, you can now do:After this, your
scores
array will be sorted by the score ascending.