Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An array is <em>not</em> a container for linked list. <a href="http://en.wikipedia.org/wiki/Linked_list" rel="nofollow noreferrer">A linked list is a list with <em>linked</em> objects</a>, not a list with objects that have relationships. Basically, what you've got is the worst of the two containers. I would try to transform that structure into some other data container; a real linked list never needs to be sorted the way you need to sort your data.</p> <p>The good way would involve something like that. I'll leave to you the way to insert objects in the middle of the list, it's not that hard.</p> <pre><code>&lt;?php class LinkedObject { var $value; var $prev; var $next; public function __construct($value, $prev = null, $next = null) { $this-&gt;value = $value; $this-&gt;prev = $prev; $this-&gt;next = $next; } public function append(LinkedObject $insertee) { $link = $this; while($link-&gt;next != null) $link = $link-&gt;next; $link-&gt;next = $insertee; $insertee-&gt;prev = $link; } public function __toString() { $str = $this-&gt;value; if($this-&gt;next != null) { $str .= " » "; $str .= $this-&gt;next; } return $str; } } $head = new LinkedObject("foo"); $head-&gt;append(new LinkedObject("bar")); $head-&gt;append(new LinkedObject("baz")); echo $head . "\n"; // gives "foo » bar » baz" ?&gt; </code></pre> <p>But, if for some occult reason you really, really need them in an array, here is what you would need:</p> <pre><code>&lt;?php function find_row($array, $id) { foreach($array as $current_row) { if($current_row['id'] === $id) return $current_row; } return null; } function what_the_heck_sort($array) { $start_record = $array[0]; $working_record = $array[0]; $result = array($working_record); while($working_record['prev'] !== null) { $working_record = find_row($array, $working_record['prev']); array_unshift($result, $working_record); } $working_record = $start_record; while($working_record['next'] !== null) { $working_record = find_row($array, $working_record['next']); array_push($result, $working_record); } return $result; } // the test code $test = array( array("foo 01", 'id' =&gt; 0, 'prev' =&gt; null, 'next' =&gt; 1), array("foo 02", 'id' =&gt; 1, 'prev' =&gt; 0, 'next' =&gt; 2), array("foo 03", 'id' =&gt; 2, 'prev' =&gt; 1, 'next' =&gt; 3), array("foo 04", 'id' =&gt; 3, 'prev' =&gt; 2, 'next' =&gt; 4), array("foo 05", 'id' =&gt; 4, 'prev' =&gt; 3, 'next' =&gt; 5), array("foo 06", 'id' =&gt; 5, 'prev' =&gt; 4, 'next' =&gt; 6), array("foo 07", 'id' =&gt; 6, 'prev' =&gt; 5, 'next' =&gt; 7), array("foo 08", 'id' =&gt; 7, 'prev' =&gt; 6, 'next' =&gt; 8), array("foo 09", 'id' =&gt; 8, 'prev' =&gt; 7, 'next' =&gt; 9), array("foo 10", 'id' =&gt; 9, 'prev' =&gt; 8, 'next' =&gt; null)); shuffle($test); print_r(what_the_heck_sort($test)); ?&gt; </code></pre> <p>But really, do yourself a favor, and do a real linked list, using objects and not arrays. The sorting method above is, in my opinion, quite decent knowing the constraints, but it's <strong>ridiculously slow</strong> because it needs to look up the array for each id.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload