Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#中定义的模式对数据表进行排序?_C#_Sorting_Datatable - Fatal编程技术网

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

如何使用c#中定义的模式对数据表进行排序?,c#,sorting,datatable,C#,Sorting,Datatable,我有这样一个数据表: type vol id ----------------------------------------- beam 120 9 foundation 340 12 column 80 20 type vol id ------------------------------------

我有这样一个数据表:

type             vol         id
-----------------------------------------
beam             120          9
foundation       340         12
column           80          20
type             vol         id
-----------------------------------------
foundation       340         12
column           80          20
beam             120          9
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;
}
需要按如下类型进行排序:

type             vol         id
-----------------------------------------
beam             120          9
foundation       340         12
column           80          20
type             vol         id
-----------------------------------------
foundation       340         12
column           80          20
beam             120          9
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;
}
如何定义“基础”优先于“柱”,“柱”优先于“梁”


编辑:对于构造调度,找到哪个元素优先于其他元素很重要,我想创建一个方法来对行(元素)进行排序。实际上,我想定义我的模式,例如,如果类型为“foundation”,则该行将上升

这里通常使用的方法是有一个隐藏列,该列未显示,但它是
优先级
——可能来自
类型
。因此,真正的表格应该是:

type             precedence    vol         id
----------------------------------------------
foundation       12            340         12
column           400           80          20
beam             801           120          9
或者,如果
type
是一个对象,则可以通过自定义比较操作执行此操作:

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;
}
公共类结构类型:IComparable,IComparable{
公共字符串名称{get;set;}
公共整数频率{get;set;}
公共重写字符串ToString()=>名称;
public int CompareTo(结构类型其他)
=>其他!=null?先行项。比较(其他。先行项):-1;
int IComparable.CompareTo(对象其他)
=>另一个是结构类型st?与(st)相比:-1;
}

您可以创建一个自定义的
IComparer
,它根据两个项目在您定义的参考顺序中的位置对它们进行比较。然后,使用比较器对表中的行进行排序

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;
    }
}

这是一个正在运行的演示:

这不仅仅是一个降序的字母顺序吗?@Steve否,我想为动态DataTable使用一个模式我如何才能做到这一点?好吧,也许写些代码?我们不是来为您做这件事的,您必须提供一些自己的想法。@HimBromBeere OK请更好地解释您的问题,因为不清楚您对动态数据表的含义以及在这种情况下什么是模式