Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort array by length and then alphabetically

Tags:

php

sorting

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?

like image 341
yainspan Avatar asked Dec 19 '22 06:12

yainspan


2 Answers

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.

like image 131
Don't Panic Avatar answered Jan 05 '23 19:01

Don't Panic


Note: I didn`t post my answer early,because @Don't Panic faster then me. However,I want to add some explanation to his answer ( hope, it will be useful for more understanding).

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

Ok. Function usort waits from custom comparison function next (from docs):

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Ok, rewrite @Don't Panic code to this view (accoding the condition above):

usort($array, function($a, $b) {
    //  SORT_ORDER_CONDITION_#1
    //  equals -> going to next by order sort-condition
    //  in our case "sorting alphabetically"
    if (strlen($a) == strlen($b)){
        // SORT_ORDER_CONDITION_#2
        if (strcmp($a,$b)==0) // equals - last sort-condition? Return 0 ( in our case - yes)
            return 0; //
       return (strcmp($a,$b)) ? -1 : 1;
    }else{
       return (strlen($a) < strlen ($b) ) ? - 1 : 1;
    }
});    

"Common sort strategy" (abstract) with multi sort-conditions in order like (CON_1,CON_2... CON_N) :

usort($array, function(ITEM_1, ITEM_2) {
    //  SORT_ORDER_CONDITION_#1
    if (COMPARING_1_EQUALS){

        // SORT_ORDER_CONDITION_#2
        if (COMPARING_2_EQUALS){ // If last con - return 0, else - going "deeper" ( to next in order)
           //... 
             // SORT_ORDER_CONDITION_#N
             if (COMPARING_N_EQUALS) // last -> equals -> return 0;
                 return 0; 
             return ( COMPARING_N_NOT_EQUALS) ? -1 : 1;                 
           //...
        }    
        return ( COMPARING_2_NOT_EQUALS) ? -1 : 1; 
    }else{
       return ( COMPARING_1_NOT_EQUALS ) ? - 1 : 1; 
    }
});           

In practise (from my exp), it's sorting unordered multidimensional-array by several conditions. You can use usort like above.

like image 28
voodoo417 Avatar answered Jan 05 '23 20:01

voodoo417