Woke up this morning thinking it could be much simpler. This time I do use a numeric sort:

#!/usr/bin/perl -w use strict; my @unordered = qw( 2HB 3HB C CA CB CG CD1 CD2 CE1 CZ CE2 HE2 HE1 HH HD1 HD2 N O OH ); # change weighting my %main = ( N => 1, CA => 2, C => 3, O => 4, S => 100, P => 200, H => 1000, ); my %greek = ( B => 1, G => 2, D => 3, E => 4, Z => 5, H => 6, ); sub sortable { my $atom = shift; $atom =~ /(\d)?(N|CA|C|O|S|P|H)(B|G|D|E|Z|H)?(\d)?/; # we may have empty groups, so quiet warnings no warnings 'uninitialized'; # make a number that sorts # keep parts at different orders of magnitude so they don't conflict $main{ $2 } + ( $greek{ $3 } * 100 ) + ( $1 * 20 ) + ( $4 * 10 ); } # operator changed to numeric comparison my @ordered = sort { sortable( $a ) <=> sortable( $b ) } @unordered; print join "\n", map { $_ ."\t". sortable( $_ ) } @ordered;

Gives:

N 1 CA 2 C 3 O 4 CB 103 CG 203 CD1 313 CD2 323 CE1 413 CE2 423 CZ 503 OH 604 2HB 1140 3HB 1160 HD1 1310 HD2 1320 HE1 1410 HE2 1420

In reply to Re: Re: sorting according to greek alphabet in roman letters by qq
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.