[英]How to Sort PHP Array Based On 2 Keys, One is Ascending And The Other is Descending
[英]How to sort an array in descending and ascending in php?
我在学生 class 中有一个由 GPA 和学生年龄组成的数组。 如果两个学生的 GPA 相同,我必须按降序排列 GPA,然后按升序排列年龄。
这是学生 class:
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;
}
}
到目前为止,我尝试在学生的 class 中创建两个函数:
public function gpaRange() {
return rsort($gpa);
}
public function ageRage() {
return sort($age);
}
您可以使用 usort() 与它自己的比较 function 对学生 class 对象数组进行排序。
$sortFctGpaAge = function($a, $b){
$cmp = $b->getGPA() <=> $a->getGPA(); //desc
if($cmp == 0) $cmp = $a->getAge() <=> $b->getAge(); //asc
return $cmp;
};
usort($students,$sortFctGpaAge);
请注意降序和升序比较中 arguments 的顺序。
这是我使用冒泡排序的解决方案,首先在 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>";
}
sort
函数在它们就地运行的方式上很奇怪。 它们不返回排序后的数组,但返回 boolean 值(无论排序是成功还是失败)。
您可以接受这一事实并在排序后返回数组:
public function gpaRange() {
rsort($this->gpa);
return $this->gpa
}
public function ageRage() {
sort($this->age);
return $this->age;
}
或者,如果您知道 arrays 始终以这样的方式排序,则当您在构造函数中收到它们时对其进行排序。
public function __construct($gpa, $age)
{
$this->gpa = $gpa;
$this->age = $age;
rsort($this->gpa);
sort($this->age);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.