Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I sort the list of data in reverse date order?

Sample XML:

<AssignmentHistory>
    <PublicTalkInfo>
        <PublicTalkNumber>21</PublicTalkNumber>
        <PublicTalkSpeaker>Name3</PublicTalkSpeaker>
        <PublicTalkCongregation>Place3</PublicTalkCongregation>
    </PublicTalkInfo>
    <PublicTalkInfo>
        <MeetingDate>2023-10-15</MeetingDate>
        <PublicTalkNumber>21</PublicTalkNumber>
        <PublicTalkSpeaker>Name</PublicTalkSpeaker>
        <PublicTalkCongregation>Place</PublicTalkCongregation>
    </PublicTalkInfo>
    <PublicTalkInfo>
        <MeetingDate>2023-11-15</MeetingDate>
        <PublicTalkNumber>21</PublicTalkNumber>
        <PublicTalkSpeaker>Name2 </PublicTalkSpeaker>
        <PublicTalkCongregation>Place</PublicTalkCongregation>
    </PublicTalkInfo>
</AssignmentHistory>

I cut it down for simplification.

public string[] GetHistoryForTalkOutline(string historyDatabase, int talknumber)
{
    List<string> list = new List<string>();
    try
    {
        XDocument doc = XDocument.Load(historyDatabase);
        var ptInfo = doc
                        .Descendants("PublicTalkInfo")
                        .Where(p => p.Element("PublicTalkNumber").Value == talknumber.ToString()).ToList();
        SimpleLog.Log(ptInfo.Count().ToString());
           foreach (var pt in ptInfo)
           {
               var date = pt.Element("MeetingDate");
               var speaker = pt.Element("PublicTalkSpeaker");
               var cong = pt.Element("PublicTalkCongregation");

               if (date != null && speaker != null && cong != null)
               {
                   DateTime objectdate = DateTime.Parse(date.Value);
                   string histitem = objectdate.ToString("d") + ", ";
                   histitem += speaker.Value + " (";
                   histitem += cong.Value + ")";
                   list.Add(histitem);
               }

           }

        return list.ToArray();
    }
    catch (Exception ex)
    {
        SimpleLog.Log(ex);
        return null;
    }
}

I am sure the code can be simplified. But how can I build my final list of strings with the content in descending date order? Note that some entries do not have the fields in.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of storing all the values in a list of strings, you can instead keep them in a format that retains the date value. For example, using a tuple:

List<(DateTime Date, string Value)> list = new List<(DateTime, string)>();

Later when you add to the list, do this:

list.Add((objectdate, histitem));

And to return the values in the order you need, use some simple Linq:

return list
    .OrderByDescending(x => x.Date)
    .Select(x => x.Value)
    .ToArray();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading