如何在php中按降序对数组进行排序?

我在学生课堂上有一个由GPA和学生年龄组成的数组。我必须按降序对GPA进行排序,然后如果两个学生的GPA相同,则年龄应递增。

这是学生班:

class Student
{
    private $gpa;
    private $age;
    public function __construct($gpa,$age)
    {
        $this->gpa = $gpa;
        $this->age = $age;
    }
    public function getGPA()
    {
        return $this->gpa;
    }
    public function getage()
    {
        return $this->age;
    }
}

到目前为止,我尝试在学生的课堂上创建两个函数:

public function gpaRange() {
        return rsort($gpa);
}

public function ageRage() {
        return sort($age);
}
charmprefect 回答:如何在php中按降序对数组进行排序?

您可以将usort()与它自己的compare函数一起使用,以对学生类对象的数组进行排序。

$sortFctGpaAge = function($a,$b){
  $cmp = $b->getGPA() <=> $a->getGPA();  //desc
  if($cmp == 0) $cmp = $a->getAge() <=> $b->getAge(); //asc 
  return $cmp;
};

usort($students,$sortFctGpaAge);

请注意比较中参数的顺序,以降序和升序排序。

,

这是我的解决方案,它利用气泡排序,首先是GPA下降,然后随着年龄的增长,找到多个GPA组。警告:这是一个比较长的解决方案,而且有点丑陋(每个人,请随时简化我的回答)。

我还包括一个更大的数组来对其进行测试。

class Student
{
    private $gpa;
    private $age;
    public function __construct($gpa,$age)
    {
        $this->gpa = $gpa;
        $this->age = $age;
    }
    public function getGPA()
    {
        return $this->gpa;
    }
    public function getAge()
    {
        return $this->age;
    }
}


$students = 
    array(new Student(4.0,22),new Student(3.3,21),new Student(2.7,new Student(3.4,19),new Student(3.6,new Student(4.0,20),new Student(3.0,20));

foreach ($students as $a)
{
    echo $a->getGPA().",".$a->getAge()."<br>";
}

echo "<br>";


//First,sort by GPA descending using bubble sort.
$sorted = false;
while ($sorted == false)
{
    $sorted = true;
    for ($x = 0; $x < sizeof($students) - 1; $x++)
    {

        //Clear temporary array.
        $temp_array = array();

        //Compare the current entry and the one right after it.
        //If in the wrong,order populate the temporary array to re-enter the values in the main array.
        if ($students[$x]->getGPA() < $students[$x+1]->getGPA())
        {
            $temp_array[0] = $students[$x+1];
            $temp_array[1] = $students[$x];

            $students[$x] = $temp_array[0];
            $students[$x+1] = $temp_array[1];
            $sorted = false;
        }
    }

    //The loop continues until all GPA entries are sorted. 
}



//Secondly,sort by age.
$counter = 0;
$target_gpa = 0;
$indices = array();
while ($counter < sizeof($students))
{
    if ($target_gpa == 0)
    {
        $target_gpa = $students[$counter]->getGPA();
        array_push($indices,$counter);
    }
    else 
    {
        if ($students[$counter]->getGPA() == $target_gpa)
        {
            array_push($indices,$counter);
        }
        else if ($students[$counter]->getGPA() != $target_gpa and sizeof($indices) < 2)
        {
            $indices = array();
            $target_gpa = $students[$counter]->getGPA();
            array_push($indices,$counter);
        }
        else if ($students[$counter]->getGPA() != $target_gpa and sizeof($indices) > 1 or $students[$counter]->getGPA() == $target_gpa and $counter + 1 == sizeof($students)- 1)
        {
            if ($counter + 1 == sizeof($students) - 1)
            {
                array_push($indices,$counter + 1);
            }

            //Bubble sort.
            $sorted = false;
            while ($sorted == false)
            {
                $sorted = true;
                for ($x = 0; $x < sizeof($indices) - 1; $x++)
                {

                    //Clear temporary array.
                    //$temp_array = array();

                    //Compare the current entry and the one right after it.
                    //If in the wrong order,populate the temporary array to re-enter the values in the main array.

                    if ($students[$indices[$x]]->getAge() > $students[$indices[$x+1]]->getAge())
                    {
                        //echo "Temp arrays: ".$temp_array[0]->getAge().",".$temp_array[1]->getAge()."<br>";
                        $temp_array[0] = $students[$indices[$x+1]];
                        $temp_array[1] = $students[$indices[$x]];

                        $students[$indices[$x]] = $temp_array[0];
                        $students[$indices[$x+1]] = $temp_array[1];
                        $sorted = false;
                    }
                }

            }

            //Replace the indices array with the new entry.
            $indices = array();
            $target_gpa = $students[$counter]->getGPA();
            array_push($indices,$counter);

        }


    }

    $counter += 1;
}

foreach ($students as $a)
{
    echo $a->getGPA().",".$a->getAge()."<br>";
}

本文链接:https://www.f2er.com/3169895.html

大家都在问