重构考虑初始文章排序的函数

refactoring a function that sorts with consideration for initial articles

我有一个函数可以执行我想要它执行的操作,但它真的非常丑陋。寻找不一定让它更快的方法,但可能让它不那么丑陋,更通用,并且总体上更易于维护。

<?php

// unordered list
$list = Array(
    0 => 'My Images',
    1 => 'Text Alignment',
    2 => 'Your New Post',
    3 => 'The More Tag',
    4 => 'The Average Post',
    5 => 'The Paginated'
);

// Desired output:
//  Array(
//      4 => 'The Average Post',
//      3 => 'The More Tag',
//      0 => 'My Images',
//      5 => 'The Paginated',
//      1 => 'Text Alignment',
//      2 => 'Your New Post'
//  )

function alphabetical_sort_with_initial_articles($array) {
    $temp_results = [];
    $results = [];
    foreach($array as $key => $value) {
        $value_arr = explode(' ', $value);
        if($value_arr[0] === 'The') { // need to make this dynamic: the|The|etc.
            $initial_article = array_shift($value_arr);
            $temp_results[$key] = array(
                implode(' ', $value_arr),
                $initial_article
            );
        } else {
            $temp_results[$key] = array(
                implode(' ', $value_arr),
                false
            );
        }
    }

    uasort($temp_results, function($a, $b) {
        return ($a[0] > $b[0]);
    });

    foreach($temp_results as $key => $value) {
        $results[$key] = trim($value[1] . ' ' . $value[0]);
    }

    return $results;
}

print_r($list);
echo "------------------------\n";
print_r(alphabetical_sort_with_initial_articles($list));

//josh@lear:~/Development/PHP/$ time php lists.php
//Array
//(
//    [0] => My Images
//    [1] => Text Alignment
//    [2] => Your New Post
//    [3] => The More Tag
//    [4] => The Average Post
//    [5] => The Paginated
//)
//------------------------
//Array
//(
//    [4] => The Average Post
//    [3] => The More Tag
//    [0] => My Images
//    [5] => The Paginated
//    [1] => Text Alignment
//    [2] => Your New Post
//)
//
//real  0m0.032s
//user  0m0.017s
//sys   0m0.013s

我不知道你从哪里得到标题,假设你一开始只是在 'the ' 上过滤。但这是非常简单的解决方案。

if (substr(strtoupper($title),0,4) == 'THE '){$key = substr($title,4);}else{$key=$title;}
$list[$key] = $title;

ksort($list);

如果向您传递一个数组:

foreach($list as $title){
    if (substr(strtoupper($title),0,4) == 'THE '){$key = substr($title,4);}
    $temp[$key] = $title;
}
ksort($temp);
$list = $temp;

如果您需要数字数组索引

foreach($list as $title){
    if (substr(strtoupper($title),0,4) == 'THE '){$key = substr($title,4);}else{$key=$title;}
    $temp[$key] = $title;
}
ksort($temp);
$list = array();
foreach ($temp as $title){
  $list[] = $title;
}

如果需要原始数值数组索引

foreach($list as $key => $title){
    if (substr(strtoupper($title),0,4) == 'THE '){$sort= substr($title,4);}else{$sort=$title;}
    $temp[$sort] = array($key,$title;);
}
ksort($temp);
$list = array();
foreach ($temp as $title){
  $list[$title[0]] = $title[1];
}

我快走了:

uasort($list, function($a, $b) {
    $a = preg_replace('/^the /i', '', $a);
    $b = preg_replace('/^the /i', '', $b);
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
});

虽然有点不清楚您要做什么。似乎您只是按字母顺序排序而忽略了 "the "s.