In David's exact example, it works and as long as the identical numbers are grouped together it works, however, if you intermix the numbers (1,8,1,4,4,3,3,9,9) it will provide an incorrect sort, or at least an ambiguous one.  In my code's case it will assume the correct order is 81439, which may be correct, or you might be expecting 18439, in which case it is incorrect.<div>
<br></div><div>My code will correctly handle duplicates in the source data to be sorted however.</div><div><br></div><div>Andy<br><br><div class="gmail_quote">On Tue, Nov 29, 2011 at 5:15 PM, Andrew Farnsworth <span dir="ltr"><<a href="mailto:farnsaw@stonedoor.com">farnsaw@stonedoor.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">David has a good point, I don't think my code will work if there are duplicate values in your predefined list.<div>
<br></div><div>Andy<div><div></div><div class="h5"><br><br><div class="gmail_quote">On Tue, Nov 29, 2011 at 5:13 PM, Andrew Farnsworth <span dir="ltr"><<a href="mailto:farnsaw@stonedoor.com" target="_blank">farnsaw@stonedoor.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Presuming your predefined sequence is fixed or known prior to the sort process, you can assign sequencial values to a hash when the keys are the predefined values in the predefined order.  Then sort using a custom sort on the values of the hash rather than the keys.  For example, in Perl, you could do it this way.<div>


<br></div><div>my @predefinedList = (1,5,8,4,3,6,9);</div><div>my %orderedHash;</div><div>my $order = 1;</div><div><br></div><div>foreach my $key (@predefinedList)</div><div>{</div><div>    $orderedHash{$key} = $order++;</div>


<div>}<br><br>my @unsortedList = (9,3,4,8,1);</div><div><br></div><div>my @sortedList = sort {$orderedHash{$a} <=> $orderedHash{$b}} @unsortedList;</div><div><br>print "@sortedList\n";</div><div><br></div>


<div>exit;</div><div><br></div><div>Andy F<div><div></div><div><br><br><div class="gmail_quote">On Tue, Nov 29, 2011 at 4:34 PM, James Courtier-Dutton <span dir="ltr"><<a href="mailto:james.dutton@gmail.com" target="_blank">james.dutton@gmail.com</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I have a problem with a special sort I need to do.<br>
I have some ideas for an efficient solution, but I wished to ask if<br>
anyone else had a better idea.<br>
My current best solution is to do a transform on the source data using<br>
hash lookups on the predefined sequence.<br>
So the source data is changed into numbers that when sorted are in<br>
numerical order, do a normal numerical sort, and then repeat the<br>
inverse of the transform at the end.<br>
<br>
Problem is as follows.<br>
Target pattern is a list of numbers in a certain predefined sequence.<br>
The source data is a set of numbers that is always a subset of the<br>
predefined sequence.<br>
I need a way to sort the source data so that the numbers appear in the<br>
same order as the predefined sequence.<br>
<br>
Eg.<br>
Predefined sequence<br>
1  5   8  4  3   6   9<br>
<br>
Source data<br>
9 3 4 8 1<br>
<br>
Result<br>
1 8 4 3 9<br>
<br>
Kind Regards<br>
<br>
James<br>
<font color="#888888">--<br>
Gllug mailing list  -  <a href="mailto:Gllug@gllug.org.uk" target="_blank">Gllug@gllug.org.uk</a><br>
<a href="http://lists.gllug.org.uk/mailman/listinfo/gllug" target="_blank">http://lists.gllug.org.uk/mailman/listinfo/gllug</a><br>
</font></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>