Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^4: Sorting Vietnamese text

by Anonymous Monk
on Dec 23, 2013 at 18:47 UTC ( #1068241=note: print w/replies, xml ) Need Help??

in reply to Re^3: Sorting Vietnamese text
in thread Sorting Vietnamese text

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 số
ấm ch
ỳ ạch
ỷ eo
ỷ lại
ỷ thế
yu nhau
yu qu

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068241]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-02-21 21:41 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.