mardi 3 septembre 2019

How to do a conditional sort with array object from laravel?

enter code hereI need to do some sorting in my controller function to display a list ranking that are sort by its average mark. However it got condition when they are two data with the same average mark, making them tie. So in order to create a tiebreaker, i need to do some condition that makes it look on the other mark in this case each data has commercial mark,usefulness mark and environmental. how do I solve this.

This is my function in Laravel controller

    public function index()
    {
        $post = Post::all();
        $post = $post->toArray();
        $temp = null;

    for($index=0; $index < count($post); $index++)   
    { 
    for($index2=0; $index2 < count($post)-1; $index2++)
    {
    if($post[$index2]['average'] < $post[$index2]['average'])
    {
        $temp = $post[$index2];
        $post[$index2] = $post[$index2+1];
        $post[$index2+1] = $temp;

    }

        else if($post[$index2]['average'] == $post[$index2]['average'])
        {
        if($post[$index2]['commercial'] < $post[$index2]['commercial'])
        {
              $temp = $post[$index2];
              $post[$index2] = $post[$index2+1];
              $post[$index2+1] = $temp;
        }
        else if($post[$index]['commercial'] == $post[$index2]['commercial'])
        {
        if($post[$index]['usefulness'] < $post[$index2]['usefulness'])
        {
               $temp = $post[$index2];
               $post[$index2] = $post[$index2+1];
               $post[$index2+1] = $temp;
        }

        else if($post[$index]['usefulness'] == $post[$index2]['usefulness'])
        {
        if($post[$index]['environmental'] < $post[$index2]['environmental'])
        {
                $temp = $post[$index2];
                $post[$index2] = $post[$index2+1];
                $post[$index2+1] = $temp;
                            }

   else if($post[$index]['environmental'] == $post[$index2]['environmental'])
   {

   }
   }
   }
   }
   else
   {

   }
   }
   }

   return $post;

This is my code in java where I found it working perfectly but i didnt know how to apply in Laravel.

for(int j=0; j < projectList.length; j++)   
     { 
        for(int k=0; k < projectList.length - 1; k++)
        {
            if(projectList[k].getAvg() < projectList[k+1].getAvg())
            {
                temp = projectList[k];
                projectList[k] = projectList[k+1];
                projectList[k+1] = temp;

            }

                else if(projectList[k].getAvg() == projectList[k+1].getAvg())
                {
                    if(projectList[k].getComm() < projectList[k+1].getComm())
                    {
                        temp = projectList[k];
                        projectList[k] = projectList[k+1];
                        projectList[k+1] = temp;
                    }

                    else if(projectList[k].getComm() == projectList[k+1].getComm())
                    {
                        if(projectList[k].getUse() < projectList[k+1].getUse())
                        {
                            temp = projectList[k];
                            projectList[k] = projectList[k+1];
                            projectList[k+1] = temp;
                        }

                        else if(projectList[k].getUse() == projectList[k+1].getUse())
                        {
                            if(projectList[k].getEnv() < projectList[k+1].getEnv())
                            {
                                 temp = projectList[k];
                                 projectList[k] = projectList[k+1];
                                 projectList[k+1] = temp;
                            }

                            else if(projectList[k].getEnv() == projectList[k+1].getEnv())
                            {

                            }
                        }
                    }

                }
            else
            {

            }
        }
    }

The sorting went totally upside down



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZCJ8Hv
via IFTTT

Aucun commentaire:

Enregistrer un commentaire