Syntax:void public DsMap::ksort ([callable $comparator])
Parameter :this function takes one parameter
$comparator,which contains a function according to which the values will be compared when sorting the copy of the map. The comparator should return the following values based on a comparison of the two values passed to it as a parameter:
- 1 if the first element is expected to be less than the second element.
- - 1 if the first element is expected to be greater than the second element.
- 0 if the first element is expected to be equal to the second element.
Returned value:this function does not return any value.The following programs illustrate the
Ds / Map::ksort()function in PHP:
Program 1:
// PHP -program to illustrate the ksort() function
// Declare the map
$map
=
new
DsMap ([1 = > 20, 3 = > 10, 2 = > thirty]);
// Sort the map element by key
$map
-> ksort();
// Show map element
print_r (
$map
);
?>
Exit:DsMap Object ( [0] = > DsPair Object ([key] = > 1 [value] = > 20) [1] = > DsPair Object ([key] = > 2 [value] = > 30) [2 ] = > DsPair Object ([key] = > 3 [value] = > 10))
Program 2:
// PHP program to illustrate the ksort() function
// Declare the map
$map
=
new
DsMap ([
" x "
= >
" Geeks "
,
"a"
= >
"for"
,
"z"
= >
"Geeks"
]);
// Sort the map element by key
$map
-> ksort();
// Show sorted item
print_r (
$map
);
?>
Exit:DsMap Object ( [0] = > DsPair Object ([key] = > a [value] = > for) [1] = > DsPair Object ([key] = > x [value] = > Geeks) [2 ] = > DsPair Object ([key] = > z [value] = > Geeks))
Link: https://www.php.net/manual/en/ds-map.ksort.php