Попытка упорядочить журналы по типу журнала или по типу logType, а затем по DateTime, но не работает. Чего мне не хватает?
public enum LogType
{
Fatal,
Error,
Warn,
Info,
Debug,
None,
}
public class Log
{
public DateTime LoggedDateTime { get; set; }
public string Message { get; set; }
public LogType LoggedType { get; set; }
public override string ToString()
{
return string.Format("LogType: {0} Datetime: {1} Message: {2}", LoggedType, LoggedDateTime, Message);
}
}
class Program
{
static void Main()
{
var logFatal = new Log
{
LoggedDateTime = new DateTime(2011, 2, 22),
Message = "Hi am a Fatal message",
LoggedType = LogType.Fatal
};
var logInfo = new Log
{
LoggedDateTime = new DateTime(2013, 2, 22),
Message = "Hi I am a Info message",
LoggedType = LogType.Info
};
var logError = new Log
{
LoggedDateTime = new DateTime(2010, 2, 22),
Message = "Hi I am a Error message",
LoggedType = LogType.Error
};
var logWarning = new Log
{
LoggedDateTime = new DateTime(2014, 2, 22),
Message = "Hi I am a Warning message",
LoggedType = LogType.Warn
};
List<Log> logs = new List<Log> { logWarning, logError, logInfo, logFatal };
Console.WriteLine("Not Ordered");
Console.WriteLine("");
logs.ForEach(x=>Console.WriteLine(x.LoggedType));
var orderedLogs = logs.OrderBy(x => x.LoggedType == LogType.Fatal)
.ThenBy(x => x.LoggedType == LogType.Error)
.ThenBy(x => x.LoggedType == LogType.Warn)
.ThenBy(x => x.LoggedDateTime).ToList();
Console.WriteLine("Ordered by logType fatal first and NOT WORKING");
Console.WriteLine("");
orderedLogs.ForEach(x=>Console.WriteLine(x.LoggedType));
Console.WriteLine("");
Console.WriteLine("Ordered by logType fatal first and NOT WORKING");//NOT WORKING
List<Log> orderedFatal = logs.OrderBy(x => x.LoggedType == LogType.Fatal).ToList();
orderedFatal.ForEach(x => Console.WriteLine(x.LoggedType));
Console.WriteLine("");
Console.WriteLine("Sorted by datetime AND WORKS"); //THIS IS THE ONLY ONE THAT WORKS
logs.Sort((x,y)=>x.LoggedDateTime.CompareTo(y.LoggedDateTime));
logs.ForEach(x => Console.WriteLine(x.LoggedDateTime));
Console.Read();
}
}
Спасибо за любые предложения