best sorting method for merge two sorted array
I have two group of array of object, say
group 1: (already sorted by title in SQL Query)[{id: 21, title: "a"},{id: 62, title: "ab"},{id: 35, title: "abc"}]
group 2: (already sorted by title in SQL Query)[{id: 23, title: "aba"},{id: 54, title: "abb"},{id: 46, title: "abe"}]
in fact, the real data is more complicated, so I can't merge and sort in MYSQL Query.
I know I can merge and sort by array_merge
and usort
but I doubt if I use for-loop to sort is faster because these 2 groups were already sorted.
Would you provide a better method for how to use for loop to sort or any other better method? Thanks.
My original query is
group 1SELECT 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')
group 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')
php mysql sorting
add a comment |
I have two group of array of object, say
group 1: (already sorted by title in SQL Query)[{id: 21, title: "a"},{id: 62, title: "ab"},{id: 35, title: "abc"}]
group 2: (already sorted by title in SQL Query)[{id: 23, title: "aba"},{id: 54, title: "abb"},{id: 46, title: "abe"}]
in fact, the real data is more complicated, so I can't merge and sort in MYSQL Query.
I know I can merge and sort by array_merge
and usort
but I doubt if I use for-loop to sort is faster because these 2 groups were already sorted.
Would you provide a better method for how to use for loop to sort or any other better method? Thanks.
My original query is
group 1SELECT 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')
group 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')
php mysql sorting
why not using join in SQL query and sort it there by title?
– Sayed Mohd Ali
Jan 3 at 7:07
add a comment |
I have two group of array of object, say
group 1: (already sorted by title in SQL Query)[{id: 21, title: "a"},{id: 62, title: "ab"},{id: 35, title: "abc"}]
group 2: (already sorted by title in SQL Query)[{id: 23, title: "aba"},{id: 54, title: "abb"},{id: 46, title: "abe"}]
in fact, the real data is more complicated, so I can't merge and sort in MYSQL Query.
I know I can merge and sort by array_merge
and usort
but I doubt if I use for-loop to sort is faster because these 2 groups were already sorted.
Would you provide a better method for how to use for loop to sort or any other better method? Thanks.
My original query is
group 1SELECT 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')
group 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')
php mysql sorting
I have two group of array of object, say
group 1: (already sorted by title in SQL Query)[{id: 21, title: "a"},{id: 62, title: "ab"},{id: 35, title: "abc"}]
group 2: (already sorted by title in SQL Query)[{id: 23, title: "aba"},{id: 54, title: "abb"},{id: 46, title: "abe"}]
in fact, the real data is more complicated, so I can't merge and sort in MYSQL Query.
I know I can merge and sort by array_merge
and usort
but I doubt if I use for-loop to sort is faster because these 2 groups were already sorted.
Would you provide a better method for how to use for loop to sort or any other better method? Thanks.
My original query is
group 1SELECT 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')
group 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')
php mysql sorting
php mysql sorting
edited Jan 3 at 7:14
Kenneth Chan
asked Jan 3 at 7:03
Kenneth ChanKenneth Chan
183115
183115
why not using join in SQL query and sort it there by title?
– Sayed Mohd Ali
Jan 3 at 7:07
add a comment |
why not using join in SQL query and sort it there by title?
– Sayed Mohd Ali
Jan 3 at 7:07
why not using join in SQL query and sort it there by title?
– Sayed Mohd Ali
Jan 3 at 7:07
why not using join in SQL query and sort it there by title?
– Sayed Mohd Ali
Jan 3 at 7:07
add a comment |
1 Answer
1
active
oldest
votes
You can try below method to sort array
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;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017722%2fbest-sorting-method-for-merge-two-sorted-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try below method to sort array
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;
}
add a comment |
You can try below method to sort array
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;
}
add a comment |
You can try below method to sort array
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;
}
You can try below method to sort array
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;
}
answered Jan 3 at 7:15
Manoj PatelManoj Patel
17519
17519
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017722%2fbest-sorting-method-for-merge-two-sorted-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why not using join in SQL query and sort it there by title?
– Sayed Mohd Ali
Jan 3 at 7:07