本文介绍了如何自定义数据表列的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,一个数据表列的值进行排序。这列包含字符串,整数或混合文本。例如:

I have a requirement to sort the values of a data table column. That column contains strings, integers or mixed texts. For example:

数据表列中包含这样的值: 23,18,12,23店,店内A1,1283,25,...

The data table column contains values like this: 23, 18, 12, store 23, store a1, 1283, 25, ...

如果我使用它会导致这个顺序 Dataview.sort()法值进行排序: 12,1283,18,23,25 ,专卖店1283,店内A1,... 但我需要这样的: 12,18,23,25,1283,23店,店内A1,...

If I sort the values by using Dataview.sort() method it results in this order: 12, 1283, 18, 23, 25, store 1283, store a1, ... but I need like this: 12, 18, 23, 25, 1283, store 23, store a1, ...

有没有达到这个要求,任何简单的方法?

Is there any simple method for attaining this requirement?

推荐答案

我认为你应该使用自然排序,让你自己的IComparer

I think you should use natural sorting and make your own IComparer

我找到的最好的算法中在这里

The best algo I found was here

只是使其成为一个通用类(如LINQ使用LINQ的作为ORDER BY需要的IComparer),如以下

Just make it a generic class(as linq uses as Linq order by takes IComparer) , like following

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

Now to apply this, use 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专卖店

Result 12, 23, 1283, store 1283

这篇关于如何自定义数据表列的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 16:49