Sort a string column by datetime in a DataTable

前端 未结 4 803
名媛妹妹
名媛妹妹 2021-01-15 14:45

I am trying to sort a DataTable on a string column by DateTime.

For various reasons, the column must be left as a string

4条回答
  •  走了就别回头了
    2021-01-15 14:54

            List rows = new List();
            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }
            rows.Sort((r1,r2)=>DateTime.Parse((string)r1["columnname"]).CompareTo(DateTime.Parse((string)r2["columnname"])));
            var clone = table.Clone();
            rows.ForEach(r => clone.Rows.Add(r.ItemArray));
            return clone;
    

提交回复
热议问题