title: Sorting date using KnockoutJS
body: <p>Firstly, I'd like to thank you for reading this post.</p>
  
  <p>I have a small problem, I am trying to sort my posts in descending order of date created. It sorts the date in descending order by the posts created today and yesterday get mixed up.</p>
  
  <p>For example: Posts created show as; 
  Created: Today, Created: Yesterday, Created: 29/09/2015, Created: 28/09/2015</p>
  
  <p>After sorting they're displayed in this order:
  Created: Yesterday,
  Created: Today,
  Created: 29/09/2015,
  Created: 28/09/2015,</p>
  
  <p>The code I am using is shown below</p>
  
  <p><code>foreach.Posts.sort(function (l, r) { return l.Created() &gt; r.Created() ? -1 : 1 })</code></p>
  
  <p>Is there a way around this ?</p>
  
  <p>Thank you.</p>
  
  <p>Updated: Added C# Class</p>
  
  <pre><code>    [HttpGet]
      public LivePostModel GetPosts(string id, string page = null, string startDate = "", string endDate = "")
      {
          Thread thread = _threadManager.GetThreadByDomain(id);           
  
          if (thread == null)
              return new LivePostModel();
  
          DateTime? dtDateFrom = null;
          DateTime? dtDateTo = null;
  
          if (string.IsNullOrEmpty(startDate) == false)
              dtDateFrom = DateTime.Parse(startDate);
  
          if (string.IsNullOrEmpty(endDate) == false)
              dtDateTo = DateTime.Parse(endDate);
  
          PostWithCount posts = _postManager.GetPosts(new PostsFilter
          {
              SubDomain = id,
              Page = string.IsNullOrEmpty(page) ? 0 : int.Parse(page),
              StartDate = dtDateFrom,
              EndDate = dtDateTo
          });
  
          IOrderedEnumerable&lt;Post&gt; sortedPosts = posts.Items.OrderByDescending(x =&gt; x.Created);
          var postsModel = new List&lt;PostModel&gt;();
  
          List&lt;string&gt; userKeys = sortedPosts
                  .Select(obj =&gt; obj.CreatedByUserId)
                  .Distinct()
                  .ToList();
  
          IList&lt;User&gt; users = _userManager.GetUsersByKeys(userKeys);
  
          foreach (Post post in sortedPosts)
          {
              User user = users.FirstOrDefault(u =&gt; u.Key == post.CreatedByUserId);
              if (user != null)
              {
                  PostModel postModel = Mapper.Map&lt;Post, PostModel&gt;(post);
                  postModel.User = GetUserDetails(postModel.User, user);
                  postsModel.Add(postModel);
              }
          }
  
          var model = new LivePostModel
          {
              Posts = postsModel.ToList(),
              CountPages = posts.Count.ToString(CultureInfo.InvariantCulture),
              CountItems = posts.CountItems.ToString(CultureInfo.InvariantCulture),
              CurrentId = thread.Key,
              Subdomain = thread.Subdomain,
              CurrentUserName = UserContextService.IsAuthenticated ? UserContextService.Key : String.Empty,
              OwnerThreadName = thread.OwnerUserId,
              OwnerThreadFullName = string.Format("{0} {1}", thread.FirstName, thread.LastName),
              CanAddPost = _threadManager.IsCurrentUserCanUploadContent(thread)
          };
          model.CurrentUserCanDelete = CheckIfUserCanDeletePost(thread);
          return model;
      }
  </code></pre>
  
  <p>JSON Data:</p>
  
  <pre><code>    "Posts":[
      {"Key":"Post_ab780bb71",
      "Header":null,
      "Message":"Post details go here ",
      "PostId":"Post_11338",
      "Created":"today",
      "Modified":"today",
      "User":{ User Details below }
  </code></pre>