簡體   English   中英

如何自定義DataTable列的排序

[英]How can I customize the sorting of a DataTable column

我需要對數據表列的值進行排序。 該列包含字符串,整數或混合文本。 例如:

數據表列包含如下值: 23, 18, 12, store 23, store a1, 1283, 25, ...

如果我使用Dataview.sort()方法對值進行排序,則會產生以下順序: 12, 1283, 18, 23, 25, store 1283, store a1, ...但我需要這樣: 12, 18, 23, 25, 1283, store 23, store a1, ...

有沒有簡單的方法來達到這個要求?

我認為你應該使用自然排序並制作自己的IComparer

我找到的最好的算法就在這里

http://www.davekoelle.com/files/AlphanumComparator.cs

只需將它設為泛型類(因為linq使用IComparer作為Linq順序),如下所示

public class AlphanumComparator<T> : IComparer<T>
    {
        private enum ChunkType { Alphanumeric, Numeric };
        private bool InChunk(char ch, char otherCh)
        {
            ChunkType type = ChunkType.Alphanumeric;

            if (char.IsDigit(otherCh))
            {
                type = ChunkType.Numeric;
            }

            if ((type == ChunkType.Alphanumeric && char.IsDigit(ch))
                || (type == ChunkType.Numeric && !char.IsDigit(ch)))
            {
                return false;
            }

            return true;
        }

        public int Compare(T x, T y)
        {
            String s1 = x as string;
            String s2 = y as string;
            if (s1 == null || s2 == null)
            {
                return 0;
            }

            int thisMarker = 0, thisNumericChunk = 0;
            int thatMarker = 0, thatNumericChunk = 0;

            while ((thisMarker < s1.Length) || (thatMarker < s2.Length))
            {
                if (thisMarker >= s1.Length)
                {
                    return -1;
                }
                else if (thatMarker >= s2.Length)
                {
                    return 1;
                }
                char thisCh = s1[thisMarker];
                char thatCh = s2[thatMarker];

                StringBuilder thisChunk = new StringBuilder();
                StringBuilder thatChunk = new StringBuilder();

                while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0])))
                {
                    thisChunk.Append(thisCh);
                    thisMarker++;

                    if (thisMarker < s1.Length)
                    {
                        thisCh = s1[thisMarker];
                    }
                }

                while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0])))
                {
                    thatChunk.Append(thatCh);
                    thatMarker++;

                    if (thatMarker < s2.Length)
                    {
                        thatCh = s2[thatMarker];
                    }
                }

                int result = 0;
                // If both chunks contain numeric characters, sort them numerically
                if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
                {
                    thisNumericChunk = Convert.ToInt32(thisChunk.ToString());
                    thatNumericChunk = Convert.ToInt32(thatChunk.ToString());

                    if (thisNumericChunk < thatNumericChunk)
                    {
                        result = -1;
                    }

                    if (thisNumericChunk > thatNumericChunk)
                    {
                        result = 1;
                    }
                }
                else
                {
                    result = thisChunk.ToString().CompareTo(thatChunk.ToString());
                }

                if (result != 0)
                {
                    return result;
                }
            }

            return 0;
        }


    }

現在應用它,使用linq

 DataTable dt = new DataTable();
            dt.TableName = "Sort";
            dt.Columns.Add("Check");
            DataRow dr = dt.NewRow();
            dr["Check"] = "12";
            dt.Rows.Add(dr);

            DataRow dr2 = dt.NewRow();
            dr2["Check"] = "1283";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3["Check"] = "store 1283";
            dt.Rows.Add(dr3);

            DataRow dr4 = dt.NewRow();
            dr4["Check"] = "23";
            dt.Rows.Add(dr4);

            DataView dv = new DataView();
            dv.Table = dt;

            AlphanumComparator<string> comparer = new AlphanumComparator<string>();
            //DataTable dtNew = dv.Table;
            DataTable dtNew = dv.Table.AsEnumerable().OrderBy(x => x.Field<string>("Check"), comparer).CopyToDataTable();
            dtNew.TableName = "NaturalSort";

            dv.Table = dtNew;

結果12,23,1283,商店1283

您無法直接根據自定義條件。 您必須編寫自己的比較代碼

看看這個問題

列的數據類型是什么。 您發布的數據類似於字母數字,即varchar

您可以使用此行代碼對數據表中的數據進行排序。 試一試。

datatable.DefaultView.Sort = "COLUMN_NAME ASC"; 

如果不是,您是否可以重新定義指定列的數據類型的問題,因為該列具有字母數字和數字值。

標准DB級或DataView類型排序不支持混合類型比較。

您可以將原始DataTable的行復制到數組中(例如使用DataTable.Rows.CopyTo() ,然后使用自定義比較器調用Array.Sort()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM