John Smith
John Smith

Reputation: 51

Function to sort an array alphabetically with uppercase letters first

I'm trying to sort an array alphabetically with uppercase letters first in the array

Example:

array(7) {
  ["H"]=>
  int(1)
  ["W"]=>
  int(1)
  ["e"]=>
  int(1)
  ["l"]=>
  int(3)
  ["o"]=>
  int(2)
  ["r"]=>
  int(1)
  ["d"]=>
  int(1)
}

My code doesn't sort with uppercase letters, only alphabetically

Here is my code:

function count_char($str) {
    $chars = str_split($str);
    $char_counter = Array();
    foreach($chars as $char) 
        if ((ord($char) >= 65 && ord($char) <= 90) || 
            (ord($char) >= 97 && ord($char) <= 122)) {
            if(!isset($char_counter[$char])) $char_counter[$char] = 1;
            else $char_counter[$char] += 1;
        }
    return $char_counter;
}

var_dump(count_char("Hello World"));

My desired output is $str, I would like alphabetizing the uppers, then alphabetizing the lowers

Upvotes: 2

Views: 1047

Answers (2)

mickmackusa
mickmackusa

Reputation: 48100

ksort() will do. You should only call ord() once and just store the result to minimize function calls. ...or better just call ctype_alpha() to ensure you are only storing letters. I recommend adding curly brackets for improved readability.

Code: (Demo)

function count_char($str) {
    $chars = str_split($str);
    $char_counter = array();
    foreach($chars as $char) {
        if (ctype_alpha($char)) {
            if (!isset($char_counter[$char])) {
                $char_counter[$char] = 1;
            } else {
                ++$char_counter[$char];
            }
        }
    }
    ksort($char_counter);
    return $char_counter;
}

var_dump(count_char("Hello World"));

Output:

array(7) {
  ["H"]=>
  int(1)
  ["W"]=>
  int(1)
  ["d"]=>
  int(1)
  ["e"]=>
  int(1)
  ["l"]=>
  int(3)
  ["o"]=>
  int(2)
  ["r"]=>
  int(1)
}

You could also condense things like this if you aren't scared off by regex:

function count_char($str) {
    $letters = preg_split('~[^a-z]*~i', $str, -1, PREG_SPLIT_NO_EMPTY);
    if (!$letters) return [];
    $counts = array_count_values($letters);
    ksort($counters);
    return $counters;
}

var_dump(count_char("Hello World"));

Upvotes: 1

Lawrence Cherone
Lawrence Cherone

Reputation: 46660

Personally I would do it like:

<?php
$str = "Hello World";

// split the string (ignoring spaces)
$array = str_split(str_replace(' ', '', $str), 1);

// count the chars
$array = array_count_values($array);

// sort naturally
array_multisort(array_keys($array), SORT_NATURAL, $array);

print_r($array);

https://3v4l.org/aZqRb

Result:

Array
(
    [H] => 1
    [W] => 1
    [d] => 1
    [e] => 1
    [l] => 3
    [o] => 2
    [r] => 1
)

Edit: If you want to sort by value and then by key:

<?php
$str = "Hello World";

// split the string (ignoring spaces)
$array = str_split(str_replace(' ', '', $str), 1);

// count the chars
$array = array_count_values($array);

// get the keys
$keys = array_keys($array);

// sort my keys
array_multisort($array, $keys);

// combine sorted keys with array
$array = array_combine($keys, $array);

print_r($array);

https://3v4l.org/pfEin

Result:

Array
(
    [H] => 1
    [W] => 1
    [d] => 1
    [e] => 1
    [r] => 1
    [o] => 2
    [l] => 3
)

Upvotes: 2

Related Questions