Here's a simplistic mechanism that would probably benefit from using a GRT. It relies upon having a pre-ordered lookup table, here in the form of a simple space delimited string and the ordering is performed by the simple numerical comparision of the target position within the string.

#! perl -slw use strict; my $order = 'N CA C O CB CG CD1 CD2 CE1 CE2 CZ OH 2HB 3HB HD1 HD2 HE1 +HE2 HH'; my @unordered = qw[ 2HB 3HB C CA CB CG CD1 CD2 CE1 CZ CE2 HE2 HE1 HH H +D1 HD2 N O OH ]; print join' ', sort{ index( $order, $a ) <=> index( $order, $b ) } @un +ordered; __END__ P:\test>295668 N C CA O CB CG CD1 CD2 CE1 CE2 CZ OH 2HB 3HB HD1 HD2 HE1 HE2 HH

However, I suspect that you would like to not have to pre-construct the ordering of all possible compounds but would prefer their "weight" to be calculated according to your set of rules. That would be possible to do using a lookup hash mapping the symbols to weights and the some math to multiply by the number of hydrogens (terminology?).

To code this would require a clearer set of rules to work with. The only minor problem I see is parsing combined symbols -- do CA, CB, CD, CE, CZ break down into "C & A", "C & B"... etc. or are they intrinsic, but that is easily summountable.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Re: sorting according to greek alphabet in roman letters by BrowserUk
in thread sorting according to greek alphabet in roman letters by seaver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.