Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:33:31+00:00 2026-05-25T20:33:31+00:00

Trying to order logs by logtype or by logType and thenBy DateTime but does

  • 0

Trying to order logs by logtype or by logType and thenBy DateTime but does not work.What am I missing?

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

Thanks for any suggestions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:33:32+00:00Added an answer on May 25, 2026 at 8:33 pm

    The problem is that false is ordered before true.
    Thus first in your order come those that are not Fatal. Out of those first come those that are not Error etc.

    You can use inequalities to make it work:

    x => x.LoggedType != LogType.Fatal
    

    etc.

    or use OrderByDescending and ThenByDescending.

    But I’d prefer to give an order of the enum:

    public enum LogType : int
        {
            Fatal = 5,
            Error = 4,
            Warn = 3,
            Info = 2,
            Debug = 1,
            None = 0,
    }
    

    and then just use this value:

    var orderedLogs = logs.OrderByDescending(x => x.LoggedType);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to order items by a list of names that are not in
I'm trying to create an html table for order logs for customers. A customer
I'm trying order a Linq to NHibernate query by the sum of it's children.
I'm trying to order items based on an attribute value: <xsl:apply-templates select=Question> <xsl:sort order=ascending
I'm trying to order the results of the get_list_or_404 method get_list_or_404(...).order_by('name') doesn't seem to
I have a strange problem, I am trying to order the output of a
I am trying to set up my tab order on the html side of
I'm trying to test an Order entity method called AddItem and I'm trying to
I'm trying to use the DataAnnotationsModelBinder in order to use data annotations for server-side
So i am trying to do this Order.find :all, :conditions => org = 'test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.