There is no information about the origin of these data, if the answer from @Leo Caracciolo is sufficient. In general cases, such as where you are reading from the file you can use the usort
, as already indicated. However, if the DDD does not matter you can omit them:
$telefones = [
'(11) 3333-4353',
'(11) 98000-2222',
'(11) 3027-5555',
'(11) 97000-0333',
'(12) 99999-9999',
'(12) 88888-8888',
'(13) 11111-1111'
];
usort($telefones, function ($a, $b) {
return strtr(substr($a, 5), ['-' => '']) <=> strtr(substr($b, 5), ['-' => '']);
});
This will result in:
array(7) {
[0]=>
string(14) "(11) 3027-5555"
[1]=>
string(14) "(11) 3333-4353"
[2]=>
string(15) "(13) 11111-1111"
[3]=>
string(15) "(12) 88888-8888"
[4]=>
string(15) "(11) 97000-0333"
[5]=>
string(15) "(11) 98000-2222"
[6]=>
string(15) "(12) 99999-9999"
}
The idea is very simple, according to Wikipedia all Ddds have exactly 2 digits and start from 11, so we use:
substr($valor, 5);
So that we only get what is from the space, that is the first phone number from the phone (* actually it will get anything in the 5th character, no matter if it is a phone number or not*).
Then we use:
strtr($valor, ['-' => ''])
To remove the -
, the use of strtr
to the str_replace
is only performance, it tends, in these cases, to be a little faster, in addition to personal preference even, but can use which prefer.
So we use the Spaceship Operator, read more here, it returns which value is higher, somewhat similar to the strcmp
, but he just returns 1
, 0
or -1
.
If you want to list otherwise use the basic math of * (-1)
, this will cause 1
be it -1
and the same the contrary, thus ordering in a decreasing manner:
usort($telefones, function ($a, $b) {
return (strtr(substr($a, 5), ['-' => '']) <=> strtr(substr($b, 5), ['-' => ''])) * (-1);
});
Regarding the reply of @Bruno Rigolon this should be a little faster, compare this and this in your machine.
/!\ It will not bring the data, so it will "trust" any value, so if there is (aa) 123c5-abc3
, which is not a valid number, it will have no different action.
In this example you gave, how would it be ordered? "(11) 97000-0333 (11) 98000-2222 (11) 3027-5555 (11) 3333-4353" or simply "(11) 98000-2222 (11) 97000-0333 (11) 3333-4353 (11) 3027-5555"?
– eldes
Since I only used examples of initials 3 and 9, the two numbers with 9 would be in front and rest behind
– Thiago
Just following the order of 9,8,7 is already good, the rest is indifferent.
– Thiago
I don’t think regex is the best way to solve this, maybe some function that takes the second number and sorts?
– Paz
php would no longer sort this for you using ORDER BY DESC ?
– user60252