You can use ksort()
function to sort the array of $templates = get_page_templates();
. So the full code will be-
<?php
$templates = get_page_templates();
// Sort based on key
ksort($templates);
foreach ( $templates as $template_name => $template_filename ) {
echo "$template_name ($template_filename)<br />";
}
?>
Also see asort()
if you need.