Sau đây là đoạn code ngắn kiểm tra sắp xếp Tiếng Việt trên Windows sử dụng .NET Framework. Như đã đề cập, .NET sử dụng collation data của HĐH Windows. Kết quả trên Windows 7 không khác với trên Vista bởi MS đã không làm gì cả cho Vietnamese collation bug, và sai cho cả dạng dựng sẵn (precomposed) và tổ hợp (composite).
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
namespace SortDemo
{
class Program
{
// Comparing and Sorting Data for a Specific Culture
// http://msdn.microsoft.com/en-us/library/a7zyyk0c.aspx
static void Main(string[] args)
{
const string vietAlpha = "aAàÀảẢãÃáÁạẠăĂằẰẳẲẵẴắẮặẶâÂầẦẩẨẫẪấẤậẬbBcCdDđĐeEèÈẻẺẽẼéÉẹẸêÊềỀểỂễỄếẾệỆfFgGhHiIìÌỉỈĩĨíÍịỊjJkKlLmMnNoOòÒỏỎõÕóÓọỌôÔồỒổỔỗỖốỐộỘơƠờỜởỞỡỠớỚợỢpPqQrRsStTuUùÙủỦũŨúÚụỤưƯừỪửỬữỮứỨựỰvVwWxXyYỳỲỷỶỹỸýÝỵỴzZ";
char[] charArray = vietAlpha.ToCharArray();
string[] precomposed = new string[charArray.Length];
string[] composite = new string[charArray.Length];
for (int i = 0; i < charArray.Length; i++)
{
precomposed[i] = charArray[i].ToString();
composite[i] = charArray[i].ToString().Normalize(NormalizationForm.FormD);
}
// Sets the CurrentCulture to "vi-VN".
Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
// Sort the values of the Array.
Array.Sort(precomposed);
Array.Sort(composite);
StringBuilder strB = new StringBuilder();
StringBuilder strB1 = new StringBuilder();
for (int i = 0; i < precomposed.Length; i++)
{
strB.Append(precomposed[i]);
strB1.Append(composite[i]);
}
// Displays the values.
MessageBox.Show("Vietnamese alphabetical order:\n\n" + vietAlpha + "\n\nAfter sorting with CultureInfo(\"vi-VN\"):\n\nPrecomposed:\n" + strB.ToString() + "\n\nComposite:\n" + strB1.ToString(), "Vietnamese Collation Test");
}
}
}
Bookmarks