请稍侯

php排序测试题

31 August 2015
更多
上周公司内部的一个小小的竞赛题,虽然题目不是很难,但是很遗憾没有做对!
还是要有待提高啊

题目

/**
 * 本轮题目内容:
 * 给一组一百个帮砍用户的openid,
 * 请找出帮砍次数最多的前十名用户的openid,按帮砍次数从大到小排序;如果有次数相同的,则以字母顺序排序;
 * 并找出帮砍次数最少的前十名,按帮砍次数从小到大排序;如果有次数相同的,则按字母顺序排序;
 */


### 答案!!

```php

<?php
/**
 * 本轮题目内容:
 * 给一组一百个帮砍用户的openid,
 * 请找出帮砍次数最多的前十名用户的openid,按帮砍次数从大到小排序;如果有次数相同的,则以字母顺序排序;
 * 并找出帮砍次数最少的前十名,按帮砍次数从小到大排序;如果有次数相同的,则按字母顺序排序;
 */

$data = array(
'oCF_hjg2sbQpNDzh0cyZCD0axjdU',
'oCF_hjgN4_2zTYOJVOu4Pcq4aJsU',
'oCF_hjiSOV_wT2AfZmsHTES32dSE',
'oCF_hjjW-bOL9joZbz1Ruc_imU8M',
'oCF_hjkwTaI_4I9aUNbOY96oTtQM',
'oCF_hjlQCKNyNTyvQn1STxzMWetE',
'oCF_hjmFAsg-IGENsF2MelwlmLwM',
'oCF_hjmOAPzo0g6bdBqJhn7NJ44Y',
'ojjs8t-dw-hDTM0a-E9JPJSx8iD0',
'ojjs8t-IX0XDySjq8ebBYL5tu9SM',
'ojjs8t-Ju4A6IaJxg0uQOJXrYofk',
'ojjs8t-LXD-UIcKx5dLwUWIae-8k',
'ojjs8t-MxbRjvhcKvEWjYnouwTXI',
'ojjs8t-NkNtaOytiY0E5Yi6XJqI4',
'ojjs8t-p47U-1L5qWkAz0X0oIS2U',
'ojjs8t-pkKQIYDuYxk3itAMebq28',
'oCF_hjnp55Yc0TXl17wYJSxY6EK4',
'oCF_hjodJmq7ktKfCRch7oMdvy1U',
'oCF_hjpqPTiFCSa9CEYq7W9S97uU',
'oCF_hjr1OGrRcfJ0F42CZGgnvCG4',
'oCF_hjs1XMJlmaXoTgjiJq4fAark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'oCF_hjsCwy_MXMoDOtA8VyF9lgpk',
'oCF_hjr1OGrRcfJ0F42CZGgnvCG4',
'oCF_hjs1XMJlmaXoTgjiJq4fAark',
'oCF_hjsCwy_MXMoDOtA8VyF9lgpk',
'oCF_hjsolm4_ffMfNIrp22ADNa6U',
'oCF_hjt1vOYkYb_AURq7Ka2nw6oQ',
'oCF_hjt3Xc6dAmBfzqtLE4dHFsII',
'oCF_hjtrkRcQP1NhNFrKvSPvKLrI',
'ojjs8t1f3DNOiUZcdo5wvmobY3GU',
'ojjs8t1LGsU4GzEzGr1DIGF9AOhc',
'ojjs8t20ZxpEGtMNCe3F4sSjVf5M',
);

// 查找唯一数据
$user = array_unique ( $data );

// 算出结果
foreach ((array)$user as $key => $value) {
	$newUser[$value]['user'] =$value;
	$newUser[$value]['total'] = 0;
	foreach ($data as $key2 => $value2) {
		if($value2 == $value){
			$newUser[$value]['total'] +=1;
		}
	}	
}

//对结果对行字典排序
ksort($newUser);

// 重新得到数据
foreach ($newUser as $key => $value) {
	$testArr[] = $value;
}

// 排序算法
foreach ((array)$testArr as $key => $value)
 {
	$sortData[$key]['openid']  = $value['user'];
	$sortData[$key]['total']  = $value['total'];

	$sortList[$key] = $value['total'];
	$sortList2[$key] = $value['total'];
	$sortList3[$key] = $value['user'];
}

$asc = $sortData;
// 根据total 排序
array_multisort($sortList, SORT_ASC, $asc);

$desc = $sortData;
// 根据total 排序
array_multisort($sortList2, SORT_DESC, $desc);

// 从小到大
echo '---------------从小到大----------------';
foreach ($asc as $key => $value) 
{
	if($key < 10){
		echo "<br/>\n";
		$p = $key+1;
		echo '排名:'.$p."&nbsp;&nbsp;&nbsp;用户:" .$value['openid'].'&nbsp;&nbsp;&nbsp;[数量]'.$value['total']."<br>\n";
	}
}

echo '---------------从大到小----------------';
// 从大到小
foreach ($desc as $key => $value) 
{
	if($key < 10){
		echo "<br/>\n";
		$p = $key+1;
		echo '排名:'.$p."&nbsp;&nbsp;用户:" .$value['openid'].'&nbsp;&nbsp;&nbsp;[数量]'.$value['total']."<br>\n";
	}
}
?>