4.20. Sorting an Array Using a Method Instead of a Function4.20.1. ProblemYou want to define a custom sorting routine to order an array. However, instead of using a function, you want to use an object method. 4.20.2. SolutionPass in an array holding a class name and method in place of the function name:
4.20.3. DiscussionAs with a custom sort function, the object method needs to take two input arguments and return 1, 0, or -1, depending if the first parameter is larger than, equal to, or less than the second:
4.20.4. See AlsoChapter 7 for more on classes and objects; Recipe 4.18 for more on custom sorting of arrays.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|