Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://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 = ('yêu quí', 'ầm', 'ãm', 'ỳ', 'ấm chè', 'yêu 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ế
yêu nhau
yêu 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-29 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found