Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

The solution in the answer by Kevbotanswer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYYtoSortableNumber(i) -
    MMMDDYYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYYtoSortableNumber(i) -
    MMMDDYYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYYtoSortableNumber(i) -
    MMMDDYYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 
added 4 characters in body
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYtoSortableNumberMMMDDYYYYtoSortableNumber(i) -
    MMMDDYYYtoSortableNumberMMMDDYYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYtoSortableNumberMMMDDYYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYtoSortableNumberMMMDDYYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYtoSortableNumber(i) -
    MMMDDYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYYtoSortableNumber(i) -
    MMMDDYYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 
deleted 5 characters in body
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38

Although theThe solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYtoSortableNumber(i) -
    MMMDDYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 

Although the solution in the answer by Kevbot is elegant, its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYtoSortableNumber(i) -
    MMMDDYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 

The solution in the answer by Kevbot is elegant, but its application is limited to ES6 browsers with an implementation of date.parse() that conforms to the specific date format used by the OP.

Instead of adding a library such as moment.js just to avoid the date.parse() dependency, a tailored solution that will work in any JavaScript environment (including old browsers) can be made with just a few lines of code:

var dates = [
  {'August 17th 2016': [75]}, 
  {'August 1st 2016': [5]}, 
  {'August 28th 2016': [5]}
];

dates.sort(function(a, b){
  var i, j;
  for(i in a); //get datestring a
  for(j in b); //get datestring b;
  return MMMDDYYYtoSortableNumber(i) -
    MMMDDYYYtoSortableNumber(j);
});

console.log(dates);

// MMMDDYYYtoSortableNumber() converts datestrings
// such as "April 5th 1969" to 19690405.
// Month name to digit approach adapted from 
// https://gist.github.com/atk/1053863
  
function MMMDDYYYtoSortableNumber(datestring) {
  var mdy = datestring.match(/\w(\w\w)\D+(\d+)\D+(\d+)/);
  return mdy[3] * 10000 +
    '  anebarprayunulugepctovec'.search(mdy[1]) * 50 +
    mdy[2] * 1;
}

Please note that it might be safer to represent the datestrings as object values rather than object keys. They will then be easier to extract safely (and faster to access). E.g.

{label: 'August 17th 2016', data: [75]}, 
edited body
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38
Loading
edited body
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38
Loading
deleted 2 characters in body
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38
Loading
deleted 168 characters in body
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38
Loading
Source Link
Tomas Langkaas
  • 4.7k
  • 2
  • 23
  • 38
Loading