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 6929025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:15:54+00:00 2026-05-27T11:15:54+00:00

I am trying to sort a collection of models by a creation date or

  • 0

I am trying to sort a collection of models by a creation date or priority, selectable by a user.

Priority can be selected in either descending or ascending order but the models are then further sub-ordered by creation date in descending order.

Creation date can also be selected in either descending or ascending order with no further sub ordering.

Below is the code I have so far:

comparator: function(task) {
  var sorter = task.get(root.sortAttr);
  var subSorter = String.fromCharCode.apply(String,
    _.map(task.get("created_at").split(""), function (c) {
      return 0xffff - c.charCodeAt();
    })
  );

  if(!root.sortAscending) {
    console.log("desc");
    return -sorter + ", " + subSorter;
  }
  else {
    console.log("asc");
    return sorter + ", " + subSorter;
  }
}

This code works for both ascending and descending ordering of creation date, but will only ever show priorities in descending order. Can anyone tell me why this is the case and what I can do to fix that?

  • 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-27T11:15:55+00:00Added an answer on May 27, 2026 at 11:15 am

    I ended up using a purely string based approach which has yielded the result I was after and offers more flexibility in terms of being able to support string based sort attributes down the track.

    this.tasks.comparator = function(task) {
      var priority = (task.get("priority_id")).toString();
      var timestamp = task.get("created_at");
    
      if (!sortAscending) {
        if (sortAttribute == "created_at") return negateString(timestamp);
        if (sortAttribute == "priority_id") return negateString(negateString(priority) + "-" + negateString(timestamp));
      }
      else {
        if (sortAttribute == "created_at") return timestamp;
        if (sortAttribute == "priority_id") return negateString(priority) + "-" + timestamp;
      }
    }
    
    this.tasks.sort();
    

    Where the negate string function is the same one found here and reproduced below as follows:

    var negateString = function(s) {
      s = s.toLowerCase();
      s = s.split("");
      s = _.map(s, function(letter) { 
        return String.fromCharCode(-(letter.charCodeAt(0)));
      });
    
      return s.join("");
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# list collection that I'm trying to sort. The strings that
I am trying to sort an array in PHP by date and time which
I am trying to sort a two-dimensional array by a timestamp column Descending. This
I'm trying to set the sort order in the category view for magento. In
I'm trying to sort a collection derived from CollectionViewSource, which simply has SortDescriptions for
We were trying to sort a collection of FileInfo objects in .NET. We implemented
I'm trying to sort a collection by attribute_id . I thought it would be
I have a local collection that I am trying to sort with Linq but
Am trying to sort via a certain value in the function, Collection users =
I'm trying to figure out how to sort a collection of documents server side

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.