logo
Tags down

shadow

How to sort array of object based on key that is date using javascript?


By : Aparna Shah
Date : October 14 2020, 02:21 PM
Any of those help You need to use Array.sort() to sorting array. So use it and in function get key of object (date) using Object.keys() and convert it to date object and then compare it.
code :
var res = arr.sort((a,b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));
var arr = [
  {'12-11-2018': 'NA' },
  { '12-05-2018': 'NA' },
  { '12-09-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-06-2018': 'pass' } 
];
var res = arr.sort((a,b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));
console.log(res);
let validDate = obj => {
  let arr = Object.keys(obj)[0].split('-');
  return new Date([arr[2], arr[0], arr[1]].join('-'));
};
let res = arr.sort((a,b) => validDate(a) - validDate(b));
let arr = [
  {'12-11-2018': 'NA' },
  { '12-05-2018': 'NA' },
  { '12-09-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-06-2018': 'pass' } 
];
let validDate = obj => {
  let arr = Object.keys(obj)[0].split('-');
  return new Date([arr[2], arr[0], arr[1]].join('-'));
};
let res = arr.sort((a,b) => validDate(a) - validDate(b));

console.log(res);


Share : facebook icon twitter icon

Sort javascript object based on date keys


By : chintu javani
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a JavaScript object that looks like follows , This line returns a string:
code :
moment(moment(b, 'DD/MM/YYYY') - moment(a, 'DD/MM/YYYY')).format('DD/MM/YYYY')
Object.keys(testObj).sort(function(a, b) {
    return moment(b, 'DD/MM/YYYY').toDate() - moment(a, 'DD/MM/YYYY').toDate();
}).forEach(function(key) {
    orderedDates[key] = testObj[key];
})
var testObj = {
    "1/10/2015": {},
    "2/10/2015": {},
    "3/10/2015": {},
    "4/10/2015": {},
    "29/09/2015": {},
    "30/09/2015": {}
};
var orderedDates = {};
Object.keys(testObj).sort(function(a, b) {
    return moment(b, 'DD/MM/YYYY').toDate() - moment(a, 'DD/MM/YYYY').toDate();
}).forEach(function(key) {
    orderedDates[key] = testObj[key];
})
Object.keys(orderedDates).forEach(function(date) {
   document.body.innerHTML += date + "<br />"
});
<script src="http://momentjs.com/downloads/moment.js"></script>

How to Sort Javascript Object Array By Date?


By : Tim Del Chiaro
Date : March 29 2020, 07:55 AM
This might help you The answers here are on the right track but you should never use the Date constructor, or Date.parse, to parse strings. Use a library and pass the format, or write a simple function, e.g.
code :
function parseMDY(s) {
  var b = s.split('/');
  return new Date(b[2],b[0]-1,b[1]);
}

document.write(parseMDY('4/30/2016'));
entries.sort(function(a,b) {
  return parseMDY(a) - parseMDY(b);
});

Sort javascript object based on a date property


By : Engineer Torikul Isl
Date : March 29 2020, 07:55 AM
help you fix your problem I would do a mix of orderby $filter and a simple for array. Let's see it by an example:
code :
//example values..
var objects = [{exampleProp: undefined, date:new Date(3,1,1970) }, 
              {exampleProp: undefined, date:new Date(2,1,1970)}, 
              {exampleProp: undefined, date:new Date(2,1,1970)}, 
              {exampleProp: undefined, date:new Date(1,1,1970)}];


//ordering your master array by date property..
objects = $filter('orderBy', objects, 'date')($scope);

//grouping by date your master object..
var dictionary = {};
objects.forEach(function(object){
   if(dictionary[object.date] == undefined)
      dictionary[object.date] = [];

    dictionary[object.date].push(object);
});

//transforming your dictionary to an array of array....
var objectsByDate = [];
for(var date in dictionary)
  objectsByDate.push(dictionary[date]);

Sort JavaScript array of object based on date and date difference


By : George Vog
Date : March 29 2020, 07:55 AM
wish helps you this should do the trick :) here its sorting by edate (reverse), if sdates are the same..
code :
Arr = [{sdate:'03/20/2017',edate:'03/21/2017',id:1},
{sdate:'03/20/2017',edate:'03/24/2017',id:2},
{sdate:'03/20/2017',edate:'03/20/2017',id:3},
{sdate:'03/19/2017',edate:'03/20/2017',id:4}];

Arr.sort(function(a, b) {
  if(a.sdate === b.sdate){
  	return new Date(b.edate) - new Date(a.edate);
  }
  return new Date(a.sdate) - new Date(b.sdate);
});

console.log(Arr);

Javascript: Sort array based on subarray element contain string date


By : Rajashree Pradhan
Date : March 29 2020, 07:55 AM
it fixes the issue You need to convert date strings to Date object and then compare them in sort function.
code :
var complexArray = [["2018-12-06T19:48:39Z", 1, "15m 10s"],["2018-12-15T19:48:39Z", 3, "16m 25s"],["2018-12-13T16:37:04Z", 2,"21m 41s"]]
  
complexArray.sort(function(a, b){
    return new Date(a[0]) - new Date(b[0]);
});

console.log(complexArray);
Related Posts Related Posts :
  • How to get prefix name from html tag
  • javascript - Simulate a click event (tap with finger) on a button in iOS
  • How to get the value of checkboxes in a kendo grid
  • Uncaught TypeError: Object(...) is not a function at eval (global-styles.js)
  • Generate divs on scroll bottom
  • App deployed to Cloud Foundry fails to start
  • Show label and percentage in Google pie chart
  • Warn user before leaving web page if changes haven't been saved
  • Objects are not valid as a React child getting error when adding div?
  • why is callback not working in nodejs with mysql?
  • D3 v4 date ticks never display last item
  • How to assign a variable which function takes as a parameter
  • Javascript how hidden div onclick of phrase
  • Can I set state inside a useEffect hook
  • How can I draw a line to the highest datapoint in chart js?
  • Make <a> link activate expand text button
  • Django: fastest way to update the data that was once sent from template to view
  • JavaScript Scope of Vaadin's "AbstractJavaScriptComponent"
  • Firestore cloud functions summing subcollections values
  • d3.js in vue component - How to hook mouse events to elements?
  • Can't add href and class to list item through javascript
  • Jest - checking local storage is called in an async function that is being mocked
  • How do I print only the selection from my list group item?
  • Using take(1) with an Observable<boolean>?
  • Multi Step Form Javascript
  • Javascript checking if username already exists(duplicate)
  • Download text file from an external URL
  • get jquery post data in node.js
  • How to cache random results using Ember data?
  • Credentials for Microsoft.Maps.TileSource callback
  • Expand particles.js to background
  • how to receive updated props in react js?
  • How do I run my code when device is connected by usb?
  • How to make javascript function can use after dot
  • Progress bar with Tooltip message should be displayed always
  • Javascript interactive card being glitchy
  • Blank data for certain hours - hence missing in my array. Is there a way to fill in the missing hours?
  • Width changing when switching from static positioning, to fixed positioning
  • Javascript form validation(oninput)
  • Stock getJSON data in a global var but later says its undefined
  • RxJS throwError event not directed to handler?
  • Non-greedy capturing parenthesis
  • Refer to class inside of static method without using its name
  • Filter array property of object without mutating the object itself
  • How to create a local module in TypeScript
  • Django - Get "total" between two elements in HTML in real-time using a script
  • How can I filter and exclude 1 item type in React?
  • Output for javascript program is 'undefined' when trying to run in command line
  • Javascript onclick if iframe exists, remove it and create new iframe?
  • Merge objects with with missing key
  • Cannot find module 'jsonwebtoken' Node.js
  • Creating a cumulative objects using javascript
  • How to write regexp to get a paramater from URL
  • Instead of getting pushed to array I get error
  • DOM Dynamic Table calculation
  • How to execute JavaScript from file
  • jQuery last child val() undefined
  • React filter and then swap array elements
  • How do I return a not found in filter function in jQuery?
  • inner json array cannot be accessed in javascript, but can console log the complete json
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org