I am having 2 reports and combining them into a list. some of the item has null values.when i do sorting, it moves null values at the last.
Here is the sample program
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Report> reports = new List<Report>();
Report[] report1 = {
new Report{Id=10001,Date=null},
new Report{Id=10001,Date=null},
new Report{Id=10001,Date=Convert.ToDateTime("01/01/2017")}};
Report[] report2 = {
new Report{Id=10002,Date=null},
new Report{Id=10002,Date=null},
new Report{Id=10002,Date=Convert.ToDateTime("03/01/2017")}};
Report[] report3 = {
new Report{Id=10003,Date=null},
new Report{Id=10003,Date=null},
new Report{Id=10003,Date=Convert.ToDateTime("05/01/2017")}};
Report[] report4 = {
new Report{Id=10004,Date=null},
new Report{Id=10004,Date=null},
new Report{Id=10004,Date=Convert.ToDateTime("07/01/2017")}};
reports.AddRange(report1);
reports.AddRange(report2);
reports.AddRange(report3);
reports.AddRange(report4);
var report5 = new List<Report>()
{
new Report{Id=null,Date=Convert.ToDateTime("02/01/2017")},
new Report{Id=null,Date=Convert.ToDateTime("04/01/2017")},
new Report{Id=null,Date=Convert.ToDateTime("06/01/2017")},
};
reports.AddRange(report5);
foreach (var report in reports.OrderByDescending(x => x.Date))
{
Console.WriteLine("ID = " + report.Id + " " + "Date = " + report.Date);
}
Console.ReadKey();
}
}
class Report
{
public int? Id { get; set; }
public DateTime? Date { get; set; }
}
}
Sorting should be made based on the date and should not skip/move the null values of report list. Output should be as follows.
ID = 10004 Date =
ID = 10004 Date =
ID = 10004 Date = 07/01/2017
ID = Date = 06/01/2017
ID = 10003 Date =
ID = 10003 Date =
ID = 10003 Date = 05/01/2017
ID = Date = 04/01/2017
ID = 10002 Date =
ID = 10002 Date =
ID = 10002 Date = 03/01/2017
ID = Date = 02/01/2017
ID = 10001 Date =
ID = 10001 Date =
ID = 10001 Date = 01/01/2017