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<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">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">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>