按子数组值改进排序数组

时间:2012-01-04 15:40:08

标签: php

我正在尝试按子数组值对数组进行排序,我将其与uasort一起使用,但代码很难看,看起来可以做得更好,因为有很多重复的代码

如何改进我构建的这种开关/案例排序?

switch ($this->view->sort_key_num) {
  case 1: // Date
      if ($this->view->sort_key_type == 1)
        krsort($this->view->content);
    break;
  case 2: // Likes
    function sort_like(&$a, &$b) { return ($a['likes'] > $b['likes']) ? 1 : -1; }
    uasort($this->view->content, 'sort_like');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 3: // new content
    function sort_entries(&$a, &$b) { return ($a['newcontent'] > $b['newcontent']) ? 1 : -1; }
    uasort($this->view->content, 'sort_entries');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 4: // comments
    function sort_comments(&$a, &$b) { return ($a['comments'] > $b['comments']) ? 1 : -1; }
    uasort($this->view->content, 'sort_comments');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 5: // facebook
    function sort_facebook(&$a, &$b) { return ($a['facebook'] > $b['facebook']) ? 1 : -1; }
    uasort($this->view->content, 'sort_facebook');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 6: // twitter
    function sort_twitter(&$a, &$b) { return ($a['twitter'] > $b['twitter']) ? 1 : -1; }
    uasort($this->view->content, 'sort_twitter');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 7: // email
    function sort_email(&$a, &$b) { return ($a['email'] > $b['email']) ? 1 : -1; }
    uasort($this->view->content, 'sort_email');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 8: // google
    function sort_google(&$a, &$b) { return ($a['google'] > $b['google']) ? 1 : -1; }
    uasort($this->view->content, 'sort_google');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
  case 10: // views
    function sort_views(&$a, &$b) { return ($a['views'] > $b['views']) ? 1 : -1; }
    uasort($this->view->content, 'sort_views');
    if ($this->view->sort_key_type == 1)
      $this->view->content = array_reverse($this->view->content);
    break;
}

2 个答案:

答案 0 :(得分:1)

我没有完成所有的测试用例,也没有全部详细阅读,但我认为这将是对代码的一个很好的优化......

class customSorter {
    private $sortCrit = NULL;
    public function __construct($criteria){
            $this->sortCrit = $criteria;
    }
    public function sort(&$a, &$b) { return ($a[$this->sortCrit] > $b[$this->sortCrit]) ? 1 : -1; }
}

switch ($this->view->sort_key_num) {
    case 1: // Date
        if ($this->view->sort_key_type == 1){
            krsort($this->view->content);
        }
        break;

    case 2: // Likes
        uasort($this->view->content, array(new customSorter('likes'), 'sort'));
        break;

    case 3: // new content
        uasort($this->view->content, array(new customSorter('newcontent'), 'sort'));
        break;

    case 4: // comments
        uasort($this->view->content, array(new customSorter('comments'), 'sort'));
        break;

    case 5: // facebook
        uasort($this->view->content, array(new customSorter('facebook'), 'sort'));
        break;
}

//Reverse the sort?
if ($this->view->sort_key_type == 1){ 
    $this->view->content = array_reverse($this->view->content);
}

答案 1 :(得分:1)

您可以创建一个类,以根据type进行排序。

class Sorter {
    private $type;
    private $content;
    public function __construct($content)
    {
        $this->content = $content;
    }
    public function sort($type)
    {
        $this->type = $type;
        uasort($this->content, function ($a,$b) {
            return $b[$this->type] - $a[$this->type];
        });
    }
}

现在我删除了日期部分,因为这是一个特例。相反,我们分别处理日期部分。

if ($this->view->sort_key_type == 1 && $this->view->sort_key_num == 1) {
    krsort($this->view->content);
}

如果事实证明它不是我们正在使用的日期,我们创建一个我们之前定义的Sorter类的实例并将其提供给您的数据 - 请注意如何通过引用来完成。

else {
    $sorter = new Sorter(&$this->view->content);
    switch ($this->view->sort_key_num) {
        case 2: $sorter->sort('likes'); break;
        case 3: $sorter->sort('newcontent') break;
        case 4: $sorter->sort('comments'); break;
        case 5: $sorter->sort('facebook'); break;
            ...
    }
}

在你的旧代码中,你最后会反转你的数组,但这不是必需的,因为我已经颠倒了你的比较方法(你给uasort的方法),所以代码将是按相反的顺序排序。