doulou1989 2011-12-12 21:00
浏览 37
已采纳

在CodeIgniter中使用PHP对多维数组进行排序以进行排名?

I'm looking to use the results in an array and sort them using a ranking algorithm similar to say Hacker News or Reddit.

For example, paraphrasing the HN algorithm: Score = P / (T+2)^G

where,
P = points of an item (e.g. votes + comments + likes)
T = time since submission (in hours)
G = Gravity, (on HN defaults to 1.8)

From what I understand, I need to use a PHP sorting array, but the PHP manual is confusing, and the similar answers on StackOverflow have very specific answers without many comments as to what the function is doing. e.g. here, here & here.

My data looks as follows

Array
(
    [0] => Array
        (
            [post_created] => 2011-12-12 07:18:17
            [post_num_likes] => 1
            [post_num_comments] => 0
            [post_message] => Some message
            [votes] => 16
        )

    [1] => Array
        (
            [post_created] => 2011-12-11 22:17:16
            [post_num_likes] => 0
            [post_num_comments] => 4
            [post_message] => Another message
            [votes] => 21
        )

    [2] => Array
        (
            [post_created] => 2011-12-11 20:21:11
            [post_num_likes] => 1
            [post_num_comments] => 2
            [post_message] => Next message
            [votes] => 1
        )

    [3] => Array
        (
            [post_created] => 2011-12-11 20:11:47
            [post_num_likes] => 0
            [post_num_comments] => 0
            [post_message] => Something else
            [votes] => 0
        )

    [4] => Array
        (
            [post_created] => 2011-12-11 20:09:46
            [post_num_likes] => 1
            [post_num_comments] => 0
            [post_message] => Another message 
            [votes] => 5
        )

So far as I understand, I need to do something like the following:

// Send array as 2nd parameter due to way you call functions in CodeIgniter
uksort($array, array('Class_name','postrank'));

function postrank($a, $b) {
    // some sorting function here
    return strcmp($a, $b);
}

I've tried copying and pasting various functions, but as they're not so well commented, it's hard to know what's going on.

How might I go about recreating a similar post ranking sorting function with the above data?

  • 写回答

3条回答 默认 最新

  • dougang6178 2011-12-12 21:29
    关注

    amidoinitrite?

    SELECT * , (
    votes + post_num_comments + post_num_likes - 1
    ) / POW( ROUND( (
    TIME_TO_SEC( TIMEDIFF( NOW( ) , post_created ) ) / 3600 ) + 2 ) , 1.8
    ) AS score
    FROM hacker_news
    ORDER BY score DESC
    

    enter image description here

    The formula on website is actually: Score = (P-1) / (T+2) ^ G

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?