[Feature request/Pull request]: add natural sort
-
I found that there was no natural sort to this plugin, and decided to implement it myself.
Please note that I have the free version of this plugin.<plugin_dir>
refers to the location of the filter-everything plugin, in my case/var/www/html/wp-content/plugins/filter-everything
First, add these 2 lines to
<plugin_dir>
/src/Admin/FilterFields.php
‘sgetOrderByOptions
function:
–'naturalasc' => esc_html__( 'Natural «123»', 'filter-everything' ),
–'naturaldesc' => esc_html__( 'Natural
«123»
', 'filter-everything' ),
This will add the options in the user’sSort Terms by
dropdown menu.Add the following case statements to
<plugin_dir>
/src/Entities/EntityManager.php
‘ssortTerms
‘s function’s switch statement:case 'naturalasc':
usort( $terms, self::compareNatAsc('name') );
break;case 'naturaldesc':
usort( $terms, self::compareNatDesc('name') );
break;This will point
naturalasc
andnaturaldesc
to 2 new function, see next step.Add these 2 new functions to
<plugin_dir>
/src/Entities/EntityManager.php
‘ssortTerms
‘s function:public static function compareNatAsc( $key ){ return function ($a, $b) use ($key) { $value_1 = isset( $a->$key ) ? strtolower($a->$key) : 0; $value_2 = isset( $b->$key ) ? strtolower($b->$key) : 0; return strnatcmp( $value_1, $value_2 ); }; } public static function compareNatDesc( $key ){ return function ($a, $b) use ($key) { $value_1 = isset( $a->$key ) ? strtolower($a->$key) : 0; $value_2 = isset( $b->$key ) ? strtolower($b->$key) : 0; return -strnatcmp( $value_1, $value_2 ); }; }
Please update your code with this, this is a necessary sorting option for me and probably many other.
Thanks.
- You must be logged in to reply to this topic.