http://www.perlmonks.org?node_id=1068241


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 = ('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í