2016-02-24 49 views
0

我有相当复杂的laravel雄辩收集需要按投影,日期,用户名排序。php复杂laravel雄辩收集排序

我一直在谷歌搜索,但没有人问或写过这种复杂的排序。

如果我只使用desc或asc顺序和sortBy函数。它的工作原理,但如果我尝试使用desc/asc两者混淆起来..

我该如何解决这个问题???

馆藏结构

collection { 
    array (size=148) 
     0 => 
      attribute: 
       id:100, 
       date:"2015-02-03" 
      relations: 
       0 project(belongstomany relationship) 
         projectid: 1 
       1 user(belongstomany relationship) 
         username:"test" 
} 

这应该排序这样

project id(desc) date(desc) name(asc) 
9     2015-02-31 test1 
9     2015-02-30 test2 
8     2015-02-30 test2 
7     2015-02-29 test3 
6     2015-02-28 test4 
5     2015-02-27 test5 
+0

你想在这里做什么?你正在尝试创建一个新的集合还是对现有的集合进行排序? –

+0

基本上,我检索数据,并有关系的雄辩集合。我正在整理现有的一个。 – user3882878

+0

然后,如果您按多个条件对它们进行排序,结果将会混合。因为你不能保证与最大ID的人也有最大日期,也有最大名称 –

回答

1

你可以做你想做的,但你必须使用sort()方法,而不是sortBy()方法。 sort()方法将采用闭包,您可以使用它来定义自定义排序算法。基本上,如果您通过关闭sort(),它会调用PHP的usort()与您的关闭排序项目。

这只是你正在寻找什么的粗略想法。您可能需要调整它,因为您发布内容的不确定性很少。您可以将其定义为一个实际功能,以便传入sort(),或者您可以将其作为匿名函数传递给sort()

function ($a, $b) { 
    /** 
    * Your question states that project is a belongsToMany relationship. 
    * This means that project is a Collection that may contain many project 
    * objects, and you need to figure out how you want to handle that. In 
    * this case, I just take the max projectid from the Collection (max, 
    * since this field will be sorted desc). 
    * 
    * If this is really just a belongsTo, you can simplify this down to 
    * just $a->project->projectid, etc. 
    */ 
    $aFirst = $a->project->max('projectid'); 
    $bFirst = $b->project->max('projectid'); 

    /** 
    * If the projectids are equal, we have to dig down to our next comparison. 
    */  
    if ($aFirst == $bFirst) { 
     /** 
     * Since the first sort field (projectids) is equal, we have to check 
     * the second sort field. 
     */ 

     /** 
     * If the dates are equal, we have to dig down to our next comparison. 
     */ 
     if ($a->date == $b->date) { 
      /** 
      * Your question states that user is a belongsToMany relationship. 
      * This means that user is a Collection that may contain many user 
      * objects, and you need to figure out how you want to handle that. 
      * In this case, I just take the min username from the Collection 
      * (min, since this field will be sorted asc). 
      */ 
      $aThird = $a->user->min('username'); 
      $bThird = $b->user->min('username'); 

      /** 
      * If the final sort criteria is equal, return 0 to tell usort 
      * that these two array items are equal (for sorting purposes). 
      */ 
      if ($aThird == $bThird) { 
       return 0; 
      } 

      /** 
      * To sort in ascending order, return -1 when the first item 
      * is less than the second item. 
      */ 
      return ($aThird < $bThird) ? -1 : 1; 
     } 

     /** 
     * To sort in descending order, return +1 when the first item is 
     * less than the second item. 
     */ 
     return ($a->date < $b->date) ? 1 : -1; 
    } 

    /** 
    * To sort in descending order, return +1 when the first item is 
    * less than the second item. 
    */ 
    return ($aFirst < $bFirst) ? 1 : -1; 
} 

欲了解更多有关如何usort()的作品,你可以check the docs