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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:56:50+00:00 2026-05-21T05:56:50+00:00

I’d like to sort by time,day. Here is my attempt: var days = new

  • 0

I’d like to sort by time,day.
Here is my attempt:

var days = new Array();
var days['SU'] = 0;
var days['MO'] = 1;
var days['TU'] = 2;
var days['WE'] = 3;
var days['TH'] = 4;
var days['FR'] = 5;
var days['SA'] = 6;

events.sort(function(a, b)
{
    if(a['day'] != b['day'])
    {
        return (days[a['day']] < days[b['day']]) ? 1 : -1;
    }
    else if(a['time'] != b['time'])
    {
        return (a['time'] < a['time']) ? 1 : -1;
    }
    else
        return 0;
);

It’s not tested, but am I doing it correct?
(Time asc, days asc) Mon 8am, Tues 8am, Mon 9pm is the order I’m looking for.

Cheers.

events[0]['day'] = 'MO';
events[0]['time'] = 8;
events[1]['day'] = 'MO';
events[1]['time'] = 21;
events[2]['day'] = 'TU';
events[2]['time'] = 8;

My solution which seems to work thanks to @T.J. Crowder

events = new Array();
events[0] = new Array();
events[0]['day'] = 'MO';
events[0]['time'] = 8;
events[1] = new Array();
events[1]['day'] = 'MO';
events[1]['time'] = 21;
events[2] = new Array();
events[2]['day'] = 'TU';
events[2]['time'] = 8;

var days = {
    'SU': 0,
    'MO': 1,
    'TU': 2,
    'WE': 3,
    'TH': 4,
    'FR': 5,
    'SA': 6
};

events.sort(function(a, b)
{
    if (a.time != b.time)
    {
        return a.time - b.time;
    }
    else if (a.day != b.day)
    {
        return days[a.day] - days[b.day];
    }
    else
    {
        return 0;
    }
});

Condensed:

events.sort(function(a, b)
{
    return a.time != b.time
       ? a.time - b.time
       : days[a.day] - days[b.day];
});
  • 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-21T05:56:50+00:00Added an answer on May 21, 2026 at 5:56 am

    Your fundamental approach is sound. A few notes:

    1. You’re not using days as an array, so I wouldn’t make it an array. Instead:

      var days = {
          'SU': 0,
          'MO': 1,
          'TU': 2,
          'WE': 3,
          'TH': 4,
          'FR': 5,
          'SA': 6
      };
      

      Also, you don’t need those quotes since none of those strings is a keyword, so:

      var days = {
          SU: 0,
          MO: 1,
          TU: 2,
          WE: 3,
          TH: 4,
          FR: 5,
          SA: 6
      };
      

      …but you may choose to keep them as a style thing, or to defend against adding ones that are keywords later.

    2. You don’t have to use the bracketed notation to look up a property (a['day']) unless the string you’re using for the property name is dynamic or the property name is a reserved word. day is neither, so you can use the simpler dotted notation (a.day).

    3. There is no elseif in JavaScript; use else if.

    4. You can simplify this:

      return (days[a['day']] < days[b['day']]) ? 1 : -1;
      

      to

      return days[a.day] - days[b.day];
      

      ..and you may be able to do something similar with your time values, but I don’t know what they are, so… now that you’ve posted them, I do, and you can.

    5. Strongly recommend always using braces, not just when you “need” them. (None of your three branches actually needs them, but you’re only using them on two.)

    6. You’ve compared a['time'] to a['time] rather than b['time'] when checking for equality.

    7. You haven’t ended your function (missing })

    8. Since you can just subtract your time values, you don’t need your final equality check.

      So:

      events.sort(function(a, b)
      {
          if (a.day != b.day)
          {
              return days[a.day] - days[b.day];
          }
          else
          {
              return a.time - b.time;
          }
      });
      

      …or you can condense it further:

      events.sort(function(a, b)
      {
          return (a.day != b.day
                 ? days[a.day] - days[b.day]
                 : a.time - b.time);
      });
      

    Live example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT

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.