[wp-trac] [WordPress Trac] #37128: Introduce helper function `wp_list_sort()`

WordPress Trac noreply at wordpress.org
Thu Sep 8 18:14:37 UTC 2016


#37128: Introduce helper function `wp_list_sort()`
--------------------------------------+------------------
 Reporter:  flixos90                  |       Owner:
     Type:  enhancement               |      Status:  new
 Priority:  normal                    |   Milestone:  4.7
Component:  General                   |     Version:
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+------------------

Comment (by swissspidy):

 Replying to [comment:8 flixos90]:
 > > One thing that isn't obvious from me just reading the tests and the
 docs is what happens if wp_list_sort is called on an object where
 `$orderby` isn't set on each field?  Where does that fall in the sort?
 Seems like that is something that should be documented and tested so the
 code remains consistent into the future.
 >
 > If the `$orderby` field isn't set on one of the objects/arrays compared,
 the comparison is skipped, basically resulting in an equal comparison. In
 this case the next field compared (if using an array of `$orderby =>
 $order`) will decide. If all comparisons between two objects result in an
 equal check, the whole check will cause an equal result (i.e. return 0).
 Since PHP only does unstable sort, this will unfortunately randomize the
 result. However we can improve this if needed, for example by adding an
 internal `__index` property on each item in the array (and removing it
 again after the comparison) and then fallback to it to maintain the
 original order on an equal comparison.

 I don't really think we should go as far as adding internal properties and
 such and simply let PHP handle this.

 > > Additionally, what about when you want to do both asc and desc ?  For
 example, alphabetical by title, but reverse chronological? Perhaps there
 should just be a single argument that you pass in that includes both the
 field and the direction?
 >
 > In the patch you can do that already: you can specify the `$orderby`
 parameter as an array of (multiple) `$orderby => $order` pairs (in this
 case it will skip the third `$order` parameter). Is that what you meant or
 did I misunderstand you?

 What about removing that third parameter and always requiring a key =>
 value pair?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37128#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list