0

I'm using array_multisort to sort the array like this. It is working well until some ascent characters are present. The sorting is treating the ascents as a separate letter and its giving incorrect results (É and E should be treated as same letter while sorting but its not).

$array = array(
    0   => array(
        'Date_meeting' => '02/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'DIOL GUE Giova'
    ),
    1   => array(
        'Date_meeting' => '01/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'DAPP Fran'
    ),
    2   => array(
        'Date_meeting' => '03/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'DÉNÉRÉA Ferd'
    ),
    3   => array(
        'Date_meeting' => '04/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'DEPRE Fran'
    ),
    4   => array(
        'Date_meeting' => '05/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'CRAUSZ Madlei'
    ),
    5   => array(
        'Date_meeting' => '01/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'ALCYD Monsrat'
    ),
    6   => array(
        'Date_meeting' => '02/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'CORHES Pautte'
    )
);

array_multisort(array_column($array, 'benef_name'), 
                SORT_ASC, 
                array_column($array, 'Date_meeting'), 
                SORT_DESC, 
                $array
               );
echo "<pre>";
print_r($array);

When I execute this, the following comes at the last which is incorrect.

array(
        'Date_meeting' => '03/09/2021',
        'equiper_name' => 'ABERK-DEROUI Ghan',
        'benef_name' => 'DÉNÉRÉA Ferd'
    )

Here is the fiddle.

This is normal and normally when we are working with ascents we use multi byte functions. Does the array_multisort has anything to handle the multi byte strings?

Linga
  • 10,379
  • 10
  • 52
  • 104

1 Answers1

1

This works:

setlocale ( LC_COLLATE ,'fr_FR.UTF-8'); //<<< NOTE THE UTF-8

array_multisort(
  array_column($array, 'benef_name'), 
  SORT_ASC, 
  SORT_LOCALE_STRING,
    
  array_map( function($a){ 
    return date_create_from_format('d/m/Y',$a)->getTimestamp(); } ,
    array_column($array, 'Date_meeting')
    ) ,
  SORT_DESC, 
  SORT_NUMERIC,
    
  $array
  );

It puts DÉNÉRÉA Ferd after DAPP Fran and before DEPRE Ferd

The dates you have can't be sorted properly. By using array_map() function on the Date-meeting column, they convert to timestamps, whitch can be sorted as numbers.

input (benef_name, Date_meeting, equiper_name):

DIOL GUE Giova,  02/09/2021,  ABERK-DEROUI Ghan
DAPP Fran,       01/06/2021,  ABERK-DEROUI Ghan
DÉNÉRÉA Ferd,    03/09/2021,  ABERK-DEROUI Ghan
DEPRE Fran,      04/09/2021,  ABERK-DEROUI Ghan
CRAUSZ Madlei,   05/09/2021,  ABERK-DEROUI Ghan
DAPP Fran,       01/09/2021,  ABERK-DEROUI Ghan
DAPP Fran,       02/11/2021,  ABERK-DEROUI Ghan

output:

CRAUSZ Madlei,  05/09/2021,  ABERK-DEROUI Ghan
DAPP Fran,      02/11/2021,  ABERK-DEROUI Ghan
DAPP Fran,      01/09/2021,  ABERK-DEROUI Ghan
DAPP Fran,      01/06/2021,  ABERK-DEROUI Ghan
DÉNÉRÉA Ferd,   03/09/2021,  ABERK-DEROUI Ghan
DEPRE Fran,     04/09/2021,  ABERK-DEROUI Ghan
DIOL GUE Giova, 02/09/2021,  ABERK-DEROUI Ghan
 
Michel
  • 4,076
  • 4
  • 34
  • 52