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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在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);
}
答案

这是我的解决方案,它利用气泡排序,首先是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,22),
          new Student(3.4,19),
          new Student(3.6,22),
          new Student(4.0,20),
          new Student(3.0,21),
          new Student(3.0,19),
          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>";
}

另一答案

您可以将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);

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

以上是关于如何在php中按降序对数组进行排序?的主要内容,如果未能解决你的问题,请参考以下文章

在django模板中按降序对相关项目进行排序

如何使用extjs4.1在网格中按降序对带有连字符的浮点值进行排序

按降序对int数组进行排序[重复]

如何按降序对 JSON 数组元素进行排序? [复制]

如何根据特定值按降序对数组进行排序[重复]

按降序对数组进行排序的更好方法