简体   繁体   中英

sort array by length and then alphabetically

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, eg 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.

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.

This is not as short as other methods, but I would argue its more clear, and can be easily extended to cover other use cases:

$f = function ($s1, $s2) {
   $n = strlen($s1) <=> strlen($s2);
   if ($n != 0) {
      return $n;
   }
   return $s1 <=> $s2;
};
usort($array, $f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM