| ID | Desc | Priority | |:-----------|------------:|:------------:| | 1 | aabc | Priority1 | 2 | xyz | Priority3 | 3 | cba | Priority5 | 4 | fg | Priority2 | 5 | xdr | Priority4 | 6 | pqr | Priority1
嗨,我上面的表格以数组的形式存储。
Array[] Values;
Values[0]={'ID','Desc','Priority'};
Values[1]={'1','aabc','Priority1'};
Values[2]={'2','xyz','Priority3'};
Values[3]={'3','cba','Priority5'};
现在我必须根据优先级对值进行排序,以便我的结果显示为。
Values[0]={'ID','Desc','Priority'};
Values[1]={'1','aabc','Priority1'};
Values[2]={'4','fg','Priority2'};
Values[3]={'2','xyz','Priority3'};
帮我在C#中编写代码
答案 0 :(得分:1)
using System.Linq;
Values.OrderBy(a => a[2]);
没有更多的背景不能确切地说,但Values
看起来很可怕。例如,为什么不使用int
来定义优先级?
答案 1 :(得分:0)
如果您确实拥有DataTable,请尝试使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication66
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Desc", typeof(string));
dt.Columns.Add("Priority", typeof(string));
dt.Rows.Add(new object[] {1, "aabc", "Priority1"});
dt.Rows.Add(new object[] {2, "xyz", "Priority3"});
dt.Rows.Add(new object[] {3, "cba", "Priority5"});
dt.Rows.Add(new object[] {4, "fg", "Priority2"});
dt.Rows.Add(new object[] {5, "xdr", "Priority4"});
dt.Rows.Add(new object[] {6, "pqr", "Priority1"});
DataTable results = dt.AsEnumerable()
.OrderBy(x => x.Field<string>("Priority"))
.CopyToDataTable();
}
}
}