
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to sort a JavaScript object list based on a property when the property is not consistent
We have an array that contains various objects. A few objects on this array have a date field (which basically is returned as a string from the server, not a date object), while for others this field is null.
The requirement is that we have to display objects without date at top, and those with date needs to be displayed after them sorted by date field.
Also, for objects without date sorting needs to be done alphabetically.
Example
const sorter = ((a, b) => { if (typeof a.date == 'undefined' && typeof b.date != 'undefined') { return -1; } else if (typeof a.date != 'undefined' && typeof b.date == 'undefined') { return 1; } else if (typeof a.date == 'undefined' && typeof b.date == 'undefined') { return a.name.localeCompare(b.name); } else if (a.date == null && b.date != null) { return -1; } else if (a.date != null && b.date == null) { return 1; } else if (a.date == null && b.date == null) { return 0; } else { var d1 = Date.parse(a.date); var d2 = Date.parse(b.date); return d1 - d2; } });
- Related Articles
- How to sort the Processes based on their property name using PowerShell?
- map() array of object titles into a new array based on other property value JavaScript
- How to remove a property from a JavaScript object?
- How to access nested JSON property based on another property's value in JavaScript?
- How do I display a list of objects based on a specific property with MongoDB?
- Add a property to a JavaScript object constructor?
- Grouping objects based on key property in JavaScript
- Grouping on the basis of object property JavaScript
- JavaScript - Sort key value pair object based on value?
- How to check if every property on object is the same recursively in JavaScript?
- Manipulate Object to group based on Array Object List in JavaScript
- How do we remove a property from a JavaScript object? - JavaScript
- How to delete a property of an object in JavaScript?
- How do I remove a property from a JavaScript object?
- Sort object array based on another array of keys - JavaScript

Advertisements