...causes my app to crash with the following error message:
Quote:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCFNumber 0x790d940> valueForUndefinedKey:]: this class is not key value coding-compliant for the key intRank.'
Hi, The problem is that there is no relationship between the intRank and the NSNumber. The sorted array works by using a property of the object type within the array as a sort criteria. For instance, if you have an array of Widget objects, and Widget has a property called rank, you could do:
if you have an array of Widget objects, and Widget has a property called rank
If anyone is interested, the way I solved the issue of sorting an NSArray of NSNumber objects, was to create an NSObject subclass called ObjectForSorting with one property only: NSNumber *rank. The ObjectForSorting.h looks like this:
Though this reply is too late but Alternatively you can try the following:
Let's assume you already prepared an NSArray "arrayToBeSorted" with NSNumber objects which contain float Values:
Though this reply is too late but Alternatively you can try the following:
Let's assume you already prepared an NSArray "arrayToBeSorted" with NSNumber objects which contain float Values:
There are also the new code block based sort methods like sortedArrayUsingComparator: that let you provide a code block to do the comparison. That can be useful for sorting arrays of objects that don't have a compare method.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Sure, you can write your own sort code. However, the code you posted is a bubble sort, which is a "naive" sort with terrible performance. Its run-time goes up with the square of the number of elements being sorted, which is VERY bad. Try that code on an array of 10,000 strings and see what happens. It will take a long, long time.
For less than 100 elements, is is usable, but beyond that it gets too slow REALLY fast.
I dare say the system sort methods use shell-Metzler, QuickSort, or some other more efficient sort routine.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
duncan you're completely right on the subject of bubble sort performance. However this was just a simple example. I sometimes find it better to explain something with a small bit of sample code.
The true thing that worries me is that for a simple thing such as sorting an array of numbers developers are asking help :/. Even if you don't find a certain (probably) built-in function right of the bat, it's probably best to write your own function instead of searching through the documentation for hours upon hours :/. A bubble sort is simple because you can easily write your own in less than 5 minutes. If by any chance the performance might prove to be an issue then you have sound grounds to look for a better solution.
duncan you're completely right on the subject of bubble sort performance. However this was just a simple example. I sometimes find it better to explain something with a small bit of sample code.
The true thing that worries me is that for a simple thing such as sorting an array of numbers developers are asking help :/. Even if you don't find a certain (probably) built-in function right of the bat, it's probably best to write your own function instead of searching through the documentation for hours upon hours :/. A bubble sort is simple because you can easily write your own in less than 5 minutes. If by any chance the performance might prove to be an issue then you have sound grounds to look for a better solution.
i apologize if this reply sounded crude.
I'm mixed on this. On the one hand, I agree with you that its scary how people go running to the forums looking for ready-made solutions to every little problem they face.
On the other hand, sorting is actually a pretty deep subject. There are whole books written on the subject, and good sorting algorithms are not easy to write.
Yes you can write a bubble sort in less than 5 minutes, but it can take tens of minutes to sort moderate-sized arrays, where a faster sort could be done in seconds. It doesn't take long for an N squared algorithm to become unacceptably slow.
I don't think you did anybody any favors by posting a bubble sort without qualifying it as a naive sort algorithm with really, really bad performance.
Remember, a lot of utter newbies read this forum, and cut-and-paste code they find into their projects, not understanding at all what they are doing. If you don't say "Here's some simple sort code to show how it's done. You should not use this code except to sort very short lists, as it will be VERY, VERY slow."
One of the major plusses in using Apple's built-in sort methods is that they can use robust, fast, debugged sort code, and all you have to do is to provide a comparison method/block/selector. The sort method can use very sophisticated algorithms, and even adaptive logic that chooses the algorithm based on the number of items.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.