高效的排名算法(在PHP) [英] Efficient ranking algorithm (in PHP)

查看:520
本文介绍了高效的排名算法(在PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经计算出的数据员工的PHP数组(从数据库中检索)。有员工人均约10列,其中8个数字(其他2个是ID和名称)。这里有一个简短的样本:

I have a PHP array (retrieved from a DB) of employees calculated data. There are about 10 columns per employee, 8 of them numerical (the other 2 are id and name). Here's a short sample:


David: 1024, 75,  22  
Mike:  500,  100, 25  
Jeff:  700,  82,  10  

我可以很容易地排序在任何(数字)字段的阵列,以显示谁在顶部/底部,但我真的很喜欢在最后的表视图来显示由值的排名,让人们获得了吨有排序和重新排序表中得到他们想要的东西。这里的表排序的第一列的一个例子,示出在括号中的排名:

I can easily sort the array on any of the (numerical) fields to show who's at the top/bottom, but what I'd really like to show in the final table view is ranking by the value, so people won't have to sort and re-sort the table to get what they want. Here's an example of the table sorted by the first column, showing rankings in parentheses:


David: 1024 (#1), 75  (#3), 22 (#2)  
Jeff:  700  (#2), 82  (#2), 10 (#3)  
Mike:  500  (#3), 100 (#1), 25 (#1)  

现在,我知道最简单的方法就是表中排序列,使用行索引的排名和每每一列重复。我只是想知道,如果我能找到一种更有效的方式。

Now, I know the easiest approach is just to sort the table by column, use the row indexes as the ranking and repeat per every column. I just wondered if I could find a more efficient way.

我想过用有序的队列(每一个需要排名列中的一个),扫描阵列一次推值到队列中。但是:

I thought about using ordered queues (one per column that needs ranking), scanning the array once and pushing values into the queues. But:

  1. 在PHP中没有任何数据结构比其他数组(除非您使用外部添加)
  2. 我不相信这是更有效的。

任何人都可以请建议最好的方法,和/或证实我应该只是几次重新排序阵列?

Could anyone please suggest the best approach, and/or confirm I should just re-sort the array several times?

感谢您的时间!

推荐答案

好了,想来想去,我决定去了之类的每一列的路子。 对于任何人感兴趣备查,这里是我已经添加到我的课的功能 - 这就是所谓的每一次,每一列我需要排名:

Ok, after much deliberation, I decided to go the "sort every column" route. For future reference by anyone interested, here's the function I've added to my class - it's called once per every column I need ranked:

   private function calculateRankings(&$employees, $columnName) {
        $comparer = "return (\$a[$columnName][0] == \$b[$columnName][0]) ? 0 :  (\$a[$columnName][0] > \$b[$columnName][0] ? -1 : 1);";
        usort($employees, create_function('$a,$b', $comparer));
        foreach($employees as $key => &$employee) {
            $employee[$columnName][1] = $key + 1;
        }
    }

的1是由于被从零开始的键。

The +1 is due to the keys being zero-based.

您prepare此功能通过打开你需要排成2个元素的数组的每个字段:所述第一([0])包含值,和第二([1])将包含在秩结束。
即: $员工['工资'] =阵列(1550,0)。 然后调用这样的功能:
$这个 - > calculateRankings($员工,工资');

You prepare for this function by turning each field you need ranked into a 2-element array: the first ([0]) contains the value, and the second ([1]) will contain the rank in the end.
I.e.: $employees['salary'] = array(1550, 0);. You then call the function like this:
$this->calculateRankings($employees, 'salary');.

我真诚地希望这可以帮助别人,有一天。感谢所有响应/评议人!

I sincerely hope this helps someone, someday. Thanks to all responders/commenters!

更新4/9:我之前无法工作所提供的功能 - 有没有办法通过第三个参数(在本例中,列名)到比较器的功能。做到这一点的唯一方法就是使用一个静态类变量,或者说,我结束了一个create_function黑客。对不起,任何混乱。

UPDATE 4/9: The function I supplied before couldn't work - there's no way to pass a third parameter (in our case, the column name) into the comparer function. The only way to do it is to use a static class variable, or a create_function hack that I ended up with. Sorry for any confusion.

这篇关于高效的排名算法(在PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆