Title: [Feature request/Pull request]: add natural sort
Last modified: November 25, 2024

---

# [Feature request/Pull request]: add natural sort

 *  Resolved [CAT the Tech](https://wordpress.org/support/users/catthetech/)
 * (@catthetech)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/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`‘s `getOrderByOptions`
   function:
   – ` 'naturalasc' => esc_html__( 'Natural &laquo;123&raquo;', 'filter-
   everything' ),`– ` 'naturaldesc' => esc_html__( 'Natural ``&laquo;123&raquo;`','
   filter-everything' ),This will add the options in the user’s `Sort Terms by` 
   dropdown menu.
 * Add the following case statements to `<plugin_dir>`/`src/Entities/EntityManager.
   php`‘s `sortTerms`‘s function’s switch statement:
 *     ```wp-block-code
               case 'naturalasc':            usort( $terms, self::compareNatAsc('name') );            break;
       ```
   
 *     ```wp-block-code
               case 'naturaldesc':            usort( $terms, self::compareNatDesc('name') );            break;
       ```
   
 * This will point `naturalasc` and `naturaldesc` to 2 new function, see next step.
 * Add these 2 new functions to `<plugin_dir>`/`src/Entities/EntityManager.php`‘
   s `sortTerms`‘s function:
 *     ```wp-block-code
       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.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Support [fesupportteam](https://wordpress.org/support/users/fesupportteam/)
 * (@fesupportteam)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/feature-request-pull-request-add-natural-sort/#post-18159181)
 * Hi [@catthetech](https://wordpress.org/support/users/catthetech/)
 * Thank you for sharing your idea. We’ll make sure to pass it on to the development
   team. So they can check it and make a final decision on it.
 * Best Regards – Victor
 *  Thread Starter [CAT the Tech](https://wordpress.org/support/users/catthetech/)
 * (@catthetech)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/feature-request-pull-request-add-natural-sort/#post-18159822)
 * Thank you for considering it, I really hope it’ll make it to some version in 
   the near future!
 *  Plugin Support [fesupportteam](https://wordpress.org/support/users/fesupportteam/)
 * (@fesupportteam)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/feature-request-pull-request-add-natural-sort/#post-18161309)
 * Hi [@catthetech](https://wordpress.org/support/users/catthetech/)
 * Additionally you can use this hook
 *     ```wp-block-code
       wpc_terms_before_display
       ```
   
 * in order to to apply your sorting logic. More you can read about it here – [https://filtereverything.pro/resources/hooks/#terms-before-display-hook](https://filtereverything.pro/resources/hooks/#terms-before-display-hook)
 * Best Regards – Victor
 *  Thread Starter [CAT the Tech](https://wordpress.org/support/users/catthetech/)
 * (@catthetech)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/feature-request-pull-request-add-natural-sort/#post-18162505)
 * I understand that is IS possible for a _technical_ user/programmer, but for the
   general non-programmer website developper, I believe it would be great to integrate
   it as a native option.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘[Feature request/Pull request]: add natural sort’ is closed to new replies.

 * ![](https://ps.w.org/filter-everything/assets/icon.svg?rev=3570563)
 * [Filter Everything — WordPress & WooCommerce Filters](https://wordpress.org/plugins/filter-everything/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/filter-everything/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/filter-everything/)
 * [Active Topics](https://wordpress.org/support/plugin/filter-everything/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/filter-everything/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/filter-everything/reviews/)

## Tags

 * [feature request](https://wordpress.org/support/topic-tag/feature-request/)

 * 4 replies
 * 2 participants
 * Last reply from: [CAT the Tech](https://wordpress.org/support/users/catthetech/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/feature-request-pull-request-add-natural-sort/#post-18162505)
 * Status: resolved