我有两组对象数组,例如
组1 :(已在SQL查询中按标题排序)
[{id: 21, title: "a"},{id: 62, title: "ab"},{id: 35, title: "abc"}]
第2组:(已在SQL查询中按标题排序)
[{id: 23, title: "aba"},{id: 54, title: "abb"},{id: 46, title: "abe"}]
实际上,实际数据更加复杂,因此我无法在MYSQL Query中进行合并和排序。
我知道我可以按array_merge
和usort
进行合并和排序
但是我怀疑是否使用for循环进行排序会更快,因为这两个组已经进行了排序。
请问您提供一种更好的方法来使用for循环排序还是任何其他更好的方法?谢谢。
我的原始查询是
第1组
SELECT a.*, c.state, c.version, c.len, c.price, GROUP_CONCAT(d.tag) AS tag,
e.keywords, e.description, e.cat, e.cover, f.url AS cover_url, f.server AS cover_server, h.url AS thumbnail_url, h.zip AS thumbnail_zip
FROM ro_final_entries a
LEFT JOIN ro_book_tag b ON b.bid = a.id AND a.id NOT IN (SELECT bookID FROM ro_app_bookshelf WHERE specify=1)
INNER JOIN ro_version c ON c.id = a.id AND (c.state = ?)
LEFT JOIN ro_final_tag d ON b.tid = d.id
LEFT JOIN ro_final_book_info e ON e.id = a.id
LEFT JOIN ro_assets f ON substr(SUBSTRING_INDEX(e.cover, '/', 4),10) = f.id
LEFT JOIN `ro_final_entries` g ON g.pid = a.id
RIGHT JOIN `ro_final_thumbnail` h ON h.bid = g.id AND h.bid AND (h.pid = 'cover' OR (h.page_index = 0 AND h.pid <> 'cover'))
WHERE (a.type = 'book')
第2组:
SELECT a.*, c.state, c.version, c.len, c.price, GROUP_CONCAT(d.tag) AS tag,
e.keywords, e.description, e.cat, e.cover, f.url AS cover_url, f.server AS cover_server, GROUP_CONCAT(i.url) AS thumbnail_url, GROUP_CONCAT(i.zip) AS thumbnail_zip
FROM ro_final_entries a
LEFT JOIN ro_book_tag b ON b.bid = a.id AND a.id NOT IN (SELECT bookID FROM ro_app_bookshelf WHERE specify=1)
INNER JOIN ro_version c ON c.id = a.id
LEFT JOIN ro_final_tag d ON b.tid = d.id
LEFT JOIN ro_final_book_info e ON e.id = a.id
LEFT JOIN ro_assets f ON substr(SUBSTRING_INDEX(e.cover, '/', 4),10) = f.id
LEFT JOIN `ro_final_entries` g ON g.pid = a.id
LEFT JOIN `ro_final_entries` h ON h.pid = g.id
RIGHT JOIN `ro_final_thumbnail` i ON i.bid = h.id AND i.bid AND (i.pid = 'cover' OR (i.page_index = 0 AND i.pid <> 'cover'))
WHERE (a.type = 'collection')
答案 0 :(得分:0)
您可以尝试以下方法对数组进行排序
function sortBy($field, &$array, $direction = 'asc'){
usort($array, create_function('$a, $b', '
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b) return 0;
$direction = strtolower(trim($direction));
return ($a ' . ($direction == 'desc' ? '>' : '<') .' $b) ? -1 : 1;
'));
return true;
}