How to Sort Date and time list in C# using selenium web driver

How to perform sorting operation on the date or time list in in c# using selenium web driver. During the automation the sorting data is a common scenario you find in every project. Sorting of Integer and String list is very easy but when It comes to Date type or time type its quite tricky part. So Today I am going to show you how can you Sort Date and time list in C# using selenium web driver .

By Using “OrderBy” we can sort date by Day, Month or year. And it will return the list of “var” type.

 

var sortedList  =    DateList.OrderBy(x=> x.Day);

 

In the below code, I am going to create custom method to sort a date. Which will help you to sort the date.

Sort Date and time list in C# using selenium web driver

public List<string> SortDate(IList<IWebElement> DateList)
        {
            List<DateTime> ListInDate = new List<DateTime>();
            List<string> finalSortedDateList = new List<string>();

//Covert IWebelement type list in the Date type to sort
foreach (IWebElement ele in DateList)
            {
                DateTime oDate = Convert.ToDateTime(ele.Text.ToString());
                ListInDate.Add(oDate);
            }

var sortedList = ListInDate.OrderBy(x => x.Day);

//Covert Date Type list in the string 
foreach (DateTime ele in sortedList)
            {
                finalSortedDateList.Add(ele.ToString());
            }

//Return final sorted list in string format
return finalSortedDateList;

        }

 

Sort date list in c# using selenium step by step

Step 1) copy this method any when in your class for example in Utility class.

Step 2) Call the method by “sortDate(DateList) ” pass a list of date as a parameter. It will return a sorted date in String type.

Add a Comment

Your email address will not be published. Required fields are marked *

4 + 15 =