代码之家  ›  专栏  ›  技术社区  ›  Yousef Golnahali

如何使用c#中定义的模式对数据表进行排序[[关闭]

  •  -2
  • Yousef Golnahali  · 技术社区  · 8 年前

    我有这样一个数据表:

    type             vol         id
    -----------------------------------------
    beam             120          9
    foundation       340         12
    column           80          20
    

    需要按以下类型排序:

    type             vol         id
    -----------------------------------------
    foundation       340         12
    column           80          20
    beam             120          9
    

    Edit:对于构造调度来说,找到哪个元素优先于其他元素很重要,我想创建一个方法来对行(元素)进行排序。实际上,我想定义我的模式,例如,如果类型是“基础”,那行就行了!

    2 回复  |  直到 8 年前
        1
  •  0
  •   Brian Rogers    8 年前

    你可以定制一下 IComparer<T> 根据两个项目在您定义的参考顺序中的位置进行比较。然后,使用比较器对表中的行进行排序。

    public class PrecedenceComparer<T> : IComparer<T>
    {
        private List<T> ReferenceList { get; set; }
    
        public PrecedenceComparer(IEnumerable<T> referenceOrder)
        {
            if (referenceOrder == null)
                throw new ArgumentException(nameof(referenceOrder));
    
            ReferenceList = referenceOrder.ToList();
        }
    
        public int Compare(T x, T y)
        {
            int xIndex = ReferenceList.IndexOf(x);
            int yIndex = ReferenceList.IndexOf(y);
            if (xIndex >= 0 && yIndex >= 0) return xIndex - yIndex;
            if (xIndex >= 0) return -1;
            if (yIndex >= 0) return 1;
            return 0;
        }
    }
    

    DataTable 使用比较器就地执行,因此需要从表中获取行,使用比较器对行进行排序,然后将排序的行复制到新表中,如下所示:

    public static DataTable SortTable(DataTable table)
    {
        var comparer = new PrecedenceComparer<string>(new string[] { "foundation", "column", "beam" });
    
        // sort the rows of the original table
        var orderedRows = table.Rows.Cast<DataRow>()
            .OrderBy(row => (string)row["type"], comparer)
            .ThenBy(row => (int)row["vol"])   // secondary sort (delete this line if there isn't one)
            ;
    
        // copy the sorted data to a new table
        DataTable newTable = new DataTable();
        foreach (DataColumn col in table.Columns)
        {
            newTable.Columns.Add(col.ColumnName, col.DataType);
        }
        foreach (DataRow row in orderedRows)
        {
            DataRow newRow = newTable.NewRow();
            for (int i = 0; i < table.Columns.Count; i++)
            {
                newRow[i] = row[i];
            }
            newTable.Rows.Add(newRow);
        }
        return newTable;
    }
    

    https://dotnetfiddle.net/R5ypUo

        2
  •  0
  •   Brian Rogers    8 年前

    precedence -可能是从 type

    type             precedence    vol         id
    ----------------------------------------------
    foundation       12            340         12
    column           400           80          20
    beam             801           120          9
    

    类型

    public class StructureType : IComparable, IComparable<StructureType> {
        public string Name {get;set;}
        public int Precendence {get;set;}
        public override string ToString() => Name;
        public int CompareTo(StructureType other)
            => other != null ? Precendence.CompareTo(other.Precendence) : -1;
        int IComparable.CompareTo(object other)
            => other is StructureType st ? CompareTo(st) : -1;
    }