数组比较和排序列出c#

时间:2015-10-25 15:19:49

标签: c# sorting

我的数据表中包含“主题”列,第1行[subject]列数据表“ABC / Vesse / 11371503 / C报告”第2行[subject]列数据表“Value / BEY / 11371503 / A报告“我需要根据/来比较主题列中的值,如果/之前的值相同,我应该在下一个斜杠和排序之前寻找下一个值。

根据建议,我基于/ strSubSplit分裂。请比较一下,如何排序并添加到列表中。非常感谢。

if (ds.Tables[0].Rows.Count > 0)
{
    foreach (DataRow row in ds.Tables[0].Rows)
    {
        string strSubject = row["Subject"].ToString();
        string strEmailFrom = row["EmailFrom"].ToString();
        string strEmailTo = row["EmailTo"].ToString();
        string strEmailCC = row["EmailCc"].ToString();
        string strEmailContent = row["EmailContent"].ToString();

        // Do proper error handling here
        DateTime strCreatedOn = DateTime.Parse(row["CreatedOn"].ToString());
        string[] strSubSplit= row["Subject"].ToString().Split(new[] {'/'},StringSplitOptions.RemoveEmptyEntries);
        mailInfoList.Add(new Tuple<string, string, string, string, string, DateTime>(strSubject, strEmailFrom, strEmailTo, strEmailCC, strEmailContent, strCreatedOn));

        var newList = mailInfoList.OrderBy(x => x.Item1).ThenBy(x => x.Item6).ToList();
    }
}

2 个答案:

答案 0 :(得分:0)

第一行包含ABC / Vesse / 11371503 /C report 和第二个包含Value /BEY /11371503 /A report

尝试,

string strSubject = string.Join("/",row["Subject"]
                                   .ToString()
                                   .Split(new[] { '/' },
                                    StringSplitOptions.RemoveEmptyEntries
                               ).ToList().OrderBy(x => x));

现在更新的值包含

第1行11371503 /ABC /C report/Vesse 第2行11371503 /A report/BEY /Value

并在您的newList变量中根据主题再次排序,

 var newList = mailInfoList.OrderBy(x => x.Item1).ThenBy(x => x.Item6).ToList();

哪会给你的最终结果

第1行11371503 /A report/BEY /Value 第2行11371503 /ABC /C report/Vesse

答案 1 :(得分:0)

试试这个:

// assuming you'll already have the subjects of the emails
var subjects = new List<string>
{
    "ABC / Vesse / 11371503 /C report",
    "Value/BEY/11371503/A report"
};

var listOfItems = new List<Tuple<string, string, int, string>>();
subjects.ForEach(s =>
{
    var splits = s.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();

    // Do proper error checking before the int.Parse and make sure every array has 4 elements
    listOfItems.Add(
        new Tuple<string, string, int, string>(splits[0], 
                                                splits[1], 
                                                int.Parse(splits[2]), 
                                                splits[3]));
});

var newList = listOfItems
    .OrderBy(x => x.Item3) // the number
    .ThenBy(x => x.Item4) // {x} report
    .ToList();