简体   繁体   中英

Array comparison and sorting to list c#

I have data table with column "Subject", row 1 [subject] column in data table "ABC / Vesse / 11371503 /C report " row 2 [subject] column in data table "Value/BEY/11371503/A report" I need to compare the values inside subject column based on / and if the value before / is same,i should look for next value before next slash and sort..

As suggested, I'm splitting based on / strSubSplit. Can please help how to sort and add to the list after comparing. Thanks a lot.

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

First Row contains ABC / Vesse / 11371503 /C report and Second contains Value /BEY /11371503 /A report

Try,

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

now updated values are contains

Row 1 11371503 /ABC /C report/Vesse Row 2 11371503 /A report/BEY /Value

and in your newList variables sorted again based on the subject,

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

Which give your final result becomes

Row 1 11371503 /A report/BEY /Value Row 2 11371503 /ABC /C report/Vesse

Try this:

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM