<p>This is a php example, but an algorithm for any language would do. What I specifically want to do is bubble up the United States and Canada to the top of the list. Here is an example of the array shortened for brevity. </p>

<pre><code>array(
  0 =&gt; '-- SELECT --',
  1 =&gt; 'Afghanistan',
  2 =&gt; 'Albania',
  3 =&gt; 'Algeria',
  4 =&gt; 'American Samoa',
  5 =&gt; 'Andorra',)
</code></pre>

<p>The id's need to stay intact. So making them -1 or -2 will unfortunately not work.</p>

[{'answer_id': 44292, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 1, 'selected': False, 'text': "<p>My shortcut in similar cases is to add a space at the start of Canada and two spaces at the start of United States. If displaying these as options in a SELECT tag, the spaces are not visible but the sorting still brings them to the front.</p>\n\n<p>However, that may be a little hacky in some contexts. In Java the thing to do would be to extend StringComparator, override the compare() method making the US and Canada special cases, then sort the list (or array) passing in your new comparator as the sort algorithm.</p>\n\n<p>However I would imagine it might be simpler to just find the relevant entries in the array, remove them from the array and add them again at the start. If you are in some kind of framework which will re-sort the array, then it might not work. But in most cases that will do just fine.</p>\n\n<p>[edit] I see that you are using a hash and not an array - so it will depend on how you are doing the sorting. Could you simply put the US into the hash with a key of -2, Canada with -1 and then sort by ID instead? Not having used PHP in anger for 11 years, I don't recall whether it has built-in sorting in its hashes or if you are doing that at the application level.</p>\n"}, {'answer_id': 44351, 'author': 'Kevin Fairchild', 'author_id': 3743, 'author_profile': 'https://Stackoverflow.com/users/3743', 'pm_score': 3, 'selected': False, 'text': '<p>What I usually do in these situations is to add a separate field called DisplayOrder or something similar.  Everything defaults to, say, 1...  You then sort by DisplayOrder and then the Name.  If you want something higher or lower on the list, you can tweak the display order accordingly while keeping your normal IDs as-is.</p>\n\n<p>-- Kevin Fairchild</p>\n'}, {'answer_id': 44889, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 1, 'selected': False, 'text': '<pre><code>$a = array(\n    0 =&gt; \'- select -\',\n    1 =&gt; \'Afghanistan\',\n    2 =&gt; \'Albania\',\n    3 =&gt; \'Algeria\',\n    80 =&gt; \'USA\'\n);\n\n$temp = array();\nforeach ($a as $k =&gt; $v) {\n    $v == \'USA\'\n        ? array_unshift($temp, array($k, $v))\n        : array_push($temp, array($k, $v));\n}\nforeach ($temp as $t) {\n    list ($k, $v) = $t;\n    echo "$k =&gt; $v\\n";\n}\n</code></pre>\n\n<p>The output is then:</p>\n\n<pre><code>80 =&gt; USA\n0 =&gt; - select -\n1 =&gt; Afghanistan\n2 =&gt; Albania\n3 =&gt; Algeria\n</code></pre>\n'}, {'answer_id': 8112194, 'author': 'hakre', 'author_id': 367456, 'author_profile': 'https://Stackoverflow.com/users/367456', 'pm_score': 0, 'selected': False, 'text': '<p>You can not change the order of elements within the same array by "moving" an item around. What you can do it to build a new array that first has your favourite items and then adds anything else from the original countries array at the end:</p>\n\n<pre><code>$countries = array(\n  0 =&gt; \'-- SELECT --\',\n  1 =&gt; \'Afghanistan\',\n  2 =&gt; \'Albania\',\n  3 =&gt; \'Algeria\',\n  4 =&gt; \'American Samoa\',\n  5 =&gt; \'Andorra\',\n  22 =&gt; \'Canada\',\n  44 =&gt; \'United States\',);\n\n# tell what should be upfront (by id)\n$favourites = array(0, 44, 22);\n\n# add favourites at first\n$ordered = array();\nforeach($favourites as $id)\n{\n    $ordered[$id] = $countries[$id];\n}\n\n# add everything else\n$ordered += array_diff_assoc($countries, $ordered);\n\n# result\nprint_r($ordered);\n</code></pre>\n\n<p><a href="http://codepad.org/YQSfToYj" rel="nofollow">Demo</a></p>\n'}, {'answer_id': 56250784, 'author': 'Rahul', 'author_id': 6556397, 'author_profile': 'https://Stackoverflow.com/users/6556397', 'pm_score': 0, 'selected': False, 'text': '<p>It\'s been ages since I don\'t know how to code. But yes.</p>\n\n<pre><code>array_unshift($queue, "United States", "Canada");\nprint_r($queue);\n</code></pre>\n\n<p><a href="https://www.php.net/manual/en/function.array-unshift.php" rel="nofollow noreferrer">array_unshift</a> — Prepend one or more elements to the beginning of an array</p>\n'}]
['https://Stackoverflow.com/questions/44272', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1797/']