Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm probably completely off base here -- why does m số come before m when sorts at the same point as ? And the same question with ỳ ạch and ỷ and the other lines...

I think there's something wrong with my alphabet -- some characters being eaten perhaps -- since the tr/// has duplicates? If they're wrong, please re-input the correct alphabetical order into the tr/// lines yourself.

I'm still not sure how spaces play part in all this; I'd sort all the single-character y variants together, and before the two-syllable ones, but the order you've given does not match that and somehow my code handles it handled it even though it shouldn't have.

You should split the string into two before sorting, though. One part for the actual word, the other for the translation.

Anyway, here's a try for a correct secondary sort -- of course, I do not have any words to test it on. It sorts how I expect it to do based on what I said in 1068139, but it does not sort how you expect it to.

Also, why the hell is my tr///d not deleting characters?

use utf8;
binmode(STDOUT, ':encoding(utf-8)');

sub make_sort_order {
	my ($primary, $secondary) = @_[0, 0];
	$primary =~
		tr(aảạăaảạăắằẳẵặấầẩẫậbcdđeẻẽẹếềểễệfghiỉĩịjklmnoỏọốồổỗộơớờởỡợpqrstuủũụưứừửữựvwxyỳỷỹỵz )
		  (00000011111111111112222223456777777888888abcddddddefghijjjjjjkkkkkkllllllmnopqrrrrrrsssssstuvwwwwwwx )d;

	$secondary =~ tr#aảạăaảạăắằẳẵặấầẩẫậbcdđeẻẽẹếềểễệfghiỉĩịjklmnoỏọốồổỗộơớờởỡợpqrstuủũụưứừửữựvwxyỳỷỹỵz #\x00-\x65#d;
	return ($primary, $secondary);
}

my @words = ('yu qu', 'ầm', 'm', 'ỳ', 'ấm ch', 'yu nhau', 'm số', 'ỷ', 'ỳ ạch', 'ỷ eo', 'ỷ lại', 'ỷ thế');


print $_->[2], "\n" for
	sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] }
	map  { [ make_sort_order($_), $_ ] } @words;



## output
m
m số
ầm
ấm ch
ỳ
ỷ
ỳ ạch
ỷ eo
ỷ lại
ỷ thế
yu nhau
yu qu

In reply to Re^4: Sorting Vietnamese text by Anonymous Monk
in thread Sorting Vietnamese text by pdenisowski

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (11)
    As of 2014-09-17 08:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (70 votes), past polls