Asked  8 Months ago    Answers:  5   Viewed   54 times

I'm trying to make a way to sort words first by length, then alphabetically.

// from
$array = ["dog", "cat", "mouse", "elephant", "apple"];

// to
$array = ["cat", "dog", "apple", "mouse", "elephant"];

I've seen this answer, but it's in Java, and this answer, but it only deals with the sorting by length. I've tried sorting by length, using the code provided in the answer, and then sorting alphabetically, but then it sorts only alphabetically.

How can I sort it first by length, and then alphabetically?



You can put both of the conditions into a usort comparison function.

usort($array, function($a, $b) {
    return strlen($a) - strlen($b) ?: strcmp($a, $b);

The general strategy for sorting by multiple conditions is to write comparison expressions for each of the conditions that returns the appropriate return type of the comparison function (an integer, positive, negative, or zero depending on the result of the comparison), and evaluate them in order of your desired sort order, e.g. first length, then alphabetical.

If an expression evaluates to zero, then the two items are equal in terms of that comparison, and the next expression should be evaluated. If not, then the value of that expression can be returned as the value of the comparison function.

The other answer here appears to be implying that this comparison function does not return an integer greater than, less than, or equal to zero. It does.

Wednesday, March 31, 2021
answered 8 Months ago

Updated On July 2020

Here is the way to do it:

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_by_sku', 10, 3 );
function filter_order_get_items_by_sku( $items, $order, $types ) {
    if( count($items) > 1 ) {
        $item_skus = $sorted_items = array();

        // Loop through order line items
        foreach( $items as $items_id => $item ){
            // Check items type: for versions before Woocommerce 3.3
            if( $item->is_type('line_item') && method_exists( $item, 'get_product' ) ){
                $product = $item->get_product(); // Get the product Object
                if( is_a( $product, 'WC_Product' ) ) {
                    $item_skus[$product->get_sku()] = $items_id;

        // Only for line items when our sku array is not empty
        if( ! empty($item_skus) ) {
            // Sorting in ASC order based on SKUs;
            ksort($item_skus); // or use krsort() for DESC order

            // Loop through sorted $item_skus array
            foreach( $item_skus as $sku => $item_id ){
                // Set items in the correct order
                $sorted_items[$item_id] = $items[$item_id];
            $items = $sorted_items;
    return $items;

Code goes in function.php file of your active child theme (or active theme). Tested and works.

This will sort items by sku order ASC on backend and frontend orders and in email notifications

Also sorting items once the order is placed before data is saved to the database, could be a better way to do it.

Wednesday, March 31, 2021
answered 8 Months ago

Do this for maintaining $array in its original order

$array = array(5,4,6,8,5,3,4,6,1);
$sorted_array = $array;


Wednesday, March 31, 2021
answered 8 Months ago

Use natsort():

$array[0]= '10. xyz';
$array[1]=  '13. xyz';
$array[2]=  '2. xyz';
$array[3]=  '1a. xyz';
$array[4]=  '6. xyz';
$array[5]=  '1b. xyz';
$array[6]=  '4a. xyz';
$array[7]=  '4b. xyz';


Saturday, May 29, 2021
answered 5 Months ago

You want the php function "asort":

it sorts the array, maintaining the index associations.

Edit: I've just noticed you're using a standard array (non-associative). if you're not fussed about preserving index associations, use sort():

Monday, July 5, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :