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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:58:41+00:00 2026-06-04T09:58:41+00:00

var sorted = results.sort(function (a, b) { var nameA = ; var nameB =

  • 0
var sorted = results.sort(function (a, b) {
    var nameA = "";
    var nameB = "";
    var str1 = a.date.toLowerCase() + " " + a.time;
    var str2 = b.date.toLowerCase() + " " + b.time;

    if (a.date.toLowerCase() != "" && a.time!= "") 
     nameA = Date.parse(new Date(str1))

    if (b.date.toLowerCase() != "" && b.time!= "") 
     nameB = Date.parse(new Date(str2))

    return nameA === nameB ? 0 : nameA < nameB ? -1 : 1;

});
  1. Can anyone improve the code, if there any faults in the way i am
    sorting. Is there any better approach?
  2. How can i implement http://underscorejs.org/#sortBy for the above
    sorting.
  • 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-06-04T09:58:42+00:00Added an answer on June 4, 2026 at 9:58 am

    One major bottleneck in your code is that you’re recomputing nameA/nameB on every sort operation. Considering that each element in array may take part in several comparisons, that’s a lot of unnecessary work. You should first build sorting index using same algorithm and then sort with it instead. You can also store sort key in same object you sort so you can just check if it is here and don’t recompute it if it is.

    Another two things in your code are absolutely unnecessary: .toLowerCase on comparison with empty string and generation of useless Data objects, when Data.parse already specified explicitly as working with strings.

    I’ve made two versions of improved sort – one with helper function (prettier, more correct ideologically) and inline – may give some extra speed-up on some JS engines. They both run roughly 3000 times faster than your original: http://jsperf.com/custom-date-sort.

    (Oh, and helper function happens to be suitable for your underscore sortBy question).

    With helper:

    function fill_datetime_ms(obj){
        if (obj.date == "" || obj.time == ""){
            return obj.dateTimeMs = ""
        } else {
            return obj.dateTimeMs = Date.parse(obj.date.toLowerCase() + " " + obj.time)
        }
    }
    
    var sorted = results.sort(function (a, b) {
        var nameA = a.dateTimeMs
        var nameB = b.dateTimeMs
        if (!nameA && nameA !== "" && !nameA !== 0){
        nameA = fill_datetime_ms(a)
        }
        if (!nameB && nameB !== "" && !nameB !== 0){
            nameB = fill_datetime_ms(b)
        }
    
        return nameA === nameB ? 0 : nameA < nameB ? -1 : 1;
    });
    

    Inline:

    var sorted = results.sort(function (a, b) {
        var nameA = a.dateTimeMs
        var nameB = b.dateTimeMs
        if (!nameA && nameA !== "" && !nameA !== 0){
            if (a.date == "" || a.time == ""){
                nameA = a.dateTimeMs = ""
            } else {
                nameA = a.dateTimeMs = Date.parse(a.date.toLowerCase() + " " + a.time)
            }
        }
        if (!nameB && nameB !== "" && !nameB !== 0){
            if (b.date == "" || b.time == ""){
                nameB = b.dateTimeMs = ""
            } else {
                nameB = b.dateTimeMs = Date.parse(b.date.toLowerCase() + " " + b.time)
            }
        }
    
        return nameA === nameB ? 0 : nameA < nameB ? -1 : 1;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Lucene.NET and getting search results sorted by 'Date' field. Here are
var thumbs = document.getElementsByTagName(img); for (var i=0; i<thumbs.length; i++) { Core.addEventListener(thumbs[i], click, function() {alert(i);});
var UI$Contract$ddlForm_change = function() { //'this' is currently the drop down that fires the
var A=function(){ }; $.extend(A.prototype, { init:function(){ alert('A init'); } }); var B=function(){ }; $.extend(B.prototype,A.prototype,{
I have 4 results inside my highcharts tooltip and they are not sorted they
How does the following code sort this array to be in numerical order? var
I'm calculating intersection of 2 sets of sorted numbers in a time-critical part of
In LinqPad I have the following query statements... var genre = Anime & Animation;
Problem : The first time you visit my projects page and filter the results
I have an array that after being sorted appears like this: var arr =

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.