PDA

View Full Version : Lỗi sắp xếp Tiếng Việt trong .NET



QuanN
16-05-2009, 19:39
Hiện trong .NET, chúng ta không thể sắp xếp TV chính xác theo thứ tự abc Việt Nam bởi vì Collation data TV của Windows bị sai. Lỗi này ảnh hưởng đến các sản phẩm Office (Word, Excel, Access), SQL Server, và .NET Framework. Mong các bạn post yêu cầu MS sửa lỗi đó cho chúng ta.

Gửi yêu cầu tới Microsoft về Tiếng Việt trong Windows 7 (http://ddth.com/showthread.php?t=272339)

Bạn chỉ cần post comment/feedback trong thread Have Comments about Windows 7 RC? (Part 2 - Do not post questions in this thread) (http://social.technet.microsoft.com/Forums/en/w7itprogeneral/thread/6839df8b-b877-4534-9796-e0dd72c12192) là xong.

[=========> Bổ sung bài viết <=========]

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).


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ìÌỉỈĩĨíÍịỊjJ kKlLmMnNoOòÒỏỎõÕóÓọỌôÔồỒổỔ ỗỖốỐộỘơƠờỜởỞỡỠớỚợ 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");
}
}
}