按日期列对多维数组进行排序,如果日期相同则使用其他列值 [英] Sort multidimensional array by date column, then use other column values if dates are the same
问题描述
我有一个存储人的多维数组.
I have a multidimensional array that stores people.
Array (
id93294 => array (
Name => "Tom Anderson",
Birthday => "03/17/1975",
Hometown => 'St. Louis',
CurrentLocation => 'Mars'
),
id29349 => (array (
Name => "Tom Anderson",
Birthday => "03/17/1975",
Hometown => 'New York',
CurrentLocation => 'New York'
)
)
有点像,除了为人们提供更多信息,所以我想首先按生日排序,然后按另一个属性排序(如果他们的家乡与他们当前的位置匹配)但是一旦我对数组进行第二次排序,它就会丢失我使用生日做的第一类......
Kind of like that except with more info for the people, so I want to first sort by birthdays THEN sort by another attribute (if their hometown matches their current location) but once I do the second sort on the array it loses the first sort I did using the birthdays...
如何在不影响我之前的排序的情况下进行多次排序.
How do I sort multiple times without it messing up my previous sorts.
附言我正在使用 uasort.
P.S. I am using uasort.
推荐答案
更新
我最近回答在有关对多维数组进行排序的确定性"主题中,以更有效的方式解决了这个问题.您可以安全地跳过阅读本答案的其余部分,直接点击链接以获取更强大的解决方案.
Update
I recently answered this question in a much more capable manner in the "definitive" topic on sorting multidimensional arrays. You can safely skip reading the rest of this answer and directly follow the link for a much more capable solution.
函数 uasort
允许您定义自己的比较函数.只需将您想要的所有条件放入其中即可.
The function uasort
lets you define your own comparison function. Simply put all the criteria you want inside that.
例如,先按生日然后按姓名排序:
For example, to sort by birthday and then by name:
function comparer($first, $second) {
// First see if birthdays differ
if ($first['birthday'] < $second['birthday']) {
return -1;
}
else if ($first['birthday'] > $second['birthday']) {
return 1;
}
// OK, birthdays are equal. What else?
if ($first['name'] < $second['name']) {
return -1;
}
else if ($first['name'] > $second['name']) {
return 1;
}
// No more sort criteria. The two elements are equal.
return 0;
}
我忽略了这样一个事实,即在您的示例中,生日不是可以通过使用运算符 <
进行简单比较来排序的格式.在实践中,您会首先将它们转换为简单可比的格式.
I am ignoring the fact that in your example, the birthdays are not in a format that can be ordered by a simple comparison using the operator <
. In practice you would convert them to a trivially-comparable format first.
更新:如果你认为维护一堆这些多标准比较器会很快变得丑陋,你会发现我同意.但是这个问题可以像计算机科学中的任何其他问题一样解决:只需添加另一个抽象级别.
Update: if you think that maintaining a bunch of these multiple-criteria comparers could get ugly real fast, you find me in agreement. But this problem can be solved as any other in computer science: just add another level of abstraction.
我将在下一个示例中假设 PHP 5.3,以便使用方便的 anon 函数语法.但原则上,你可以用 create_function
做同样的事情.
I 'll be assuming PHP 5.3 for the next example, in order to use the convenient anon function syntax. But in principle, you could do the same with create_function
.
function make_comparer() {
$criteriaNames = func_get_args();
$comparer = function($first, $second) use ($criteriaNames) {
// Do we have anything to compare?
while(!empty($criteriaNames)) {
// What will we compare now?
$criterion = array_shift($criteriaNames);
// Do the actual comparison
if ($first[$criterion] < $second[$criterion]) {
return -1;
}
else if ($first[$criterion] > $second[$criterion]) {
return 1;
}
}
// Nothing more to compare with, so $first == $second
return 0;
};
return $comparer;
}
然后你可以这样做:
uasort($myArray, make_comparer('birthday', 'name'));
这个例子可能太聪明了;一般来说,我不喜欢使用不按名称接受参数的函数.但在这种情况下,使用场景是过于聪明的有力论据.
This example possibly tries to be too clever; in general I don't like to use functions that do not accept their arguments by name. But in this case, the usage scenario is a very strong argument for being too clever.
这篇关于按日期列对多维数组进行排序,如果日期相同则使用其他列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!