-2

I want to sort two models by Datetime, both of them going to one timeline so i need the newest from them to be on the start of the list timeline, I found solutions like Union models, but I want them to remain two separate lists..Is it possible?

foreach (var list1 in model.list1.OrderByDescending(a => a.created_datetime))
{
    09/18/19
}

foreach (var list2 in model.list2.OrderByDescending(a => a.created_datetime))
{
    09/19/19
}

Now I want the timeline result to be listed in the second list because it is an earlier result

Daniel
  • 403
  • 5
  • 13
  • 1
    It's not clear to me what you're asking. Are you just looking for the `.OrderBy()` extension method to sort your collections? – David Sep 19 '19 at 17:08
  • Thanks for your response, I want to sort them out by Datetime, let's say if the first list has a date that is yesterday and the second one has a date whose date today then the second record will be displayed first – Daniel Sep 19 '19 at 17:12
  • 1
    @DanielC how can you keep the lists separate _and_ blend them together? I am very unclear what you are asking – maccettura Sep 19 '19 at 17:12
  • @DanielC: Perhaps you could include a more complete example, with data, and the intended output? It's not clear how you want to keep the lists separate but sort them together. – David Sep 19 '19 at 17:13
  • Thanks for your response, this is what I am trying to figure out if there is a way to condition something like this or just unite them? – Daniel Sep 19 '19 at 17:14

2 Answers2

0

This is what you mean? (Using Linq)

var merged = list1.Union(list2).OrderByDescending(d => d.Date);

Note that this is only possible, when the two lists are the same type. Other note, that this returns an IEnumerable, not a new List, so every time you enumerate it, the merge will be calculated. You have to call ToList() at the end if you want to create a new List to prevent this, but this can be very suboptimal in performance if you have large lists.

Example program:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Lists
{
    class Program
    {
        static void Main(string[] args)
        {
            var list1 = new List<DateItem>
            {
                new DateItem{Date = DateTime.UtcNow.AddDays(-2), Name = "The day before yesterday" },
                new DateItem{Date = DateTime.UtcNow.AddDays(1), Name = "Tomorrow" }
            };

            var list2 = new List<DateItem>
            {
                new DateItem{Date = DateTime.UtcNow.AddDays(2), Name = "The day after tomorrow" },
                new DateItem{Date = DateTime.UtcNow.AddDays(-1), Name = "Yesterday" },
                new DateItem{Date = DateTime.UtcNow, Name = "Today" }
        };

            var merged = list1.Union(list2).OrderByDescending(d => d.Date);

            foreach (var day in merged)
            {
                Console.WriteLine(day.Name);
            }
        }
    }

    class DateItem
    {
        public DateTime Date { get; set; }
        public string Name { get; set; }
    }

}
LeBoucher
  • 412
  • 2
  • 8
0

Thanks for your help, the most helpful solution I found to the problem is:- Sorting the list by a JS function that runs on the uploaded content.

here is the code:

var item = $(".sort-item").sort(function (a, b) {
                var Adate = $(a).attr("data-date");
                var Bdate = $(b).attr("data-date");
                var Atime = $(a).attr("data-time");
                var Btime = $(b).attr("data-time");
                if (Adate < Bdate) return 1;
                if (Adate > Bdate) return -1;
                if (Atime < Btime) return 1;
                if (Atime > Btime) return -1;
                return 0;
           });
           $(".timeline").prepend(item);
Daniel
  • 403
  • 5
  • 13