Sort array by date - javascript [duplicate] - javascript

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Sort array of objects by single key with date value
(19 answers)
Closed 8 years ago.
I want to sort an array of objects by date, but the problem is their date is in this format - 2014-07-17T13:49:12.767Z.
Here an example of one object in this array
{
id: 578,
creationDate: "2014-07-16T20:56:04.710Z",
creationUser: "FCOUT",
modificationDate: "2014-07-17T13:49:12.767Z",
modificationUser: "FCOUT",
name: "Regra Filipe",
description: "Teste",
type: "Message",
regulation: null,
structure: 1,
deleted: false,
}
I have to sort them by modification date or creation date!

Just write a sort function as given here. In your case the compare function will just be comparing two strings , which in the date format you have mentioned should work out of the box.
ie you can compare the two dates as strings.

Mozilla sorting documentation
use the compare function
function(a, b) {
if (a.creationDate < b.creationDate) { return -1; }
if (a.creationDate > b.creationDate) { return 1; }
return 0;
}
Your date format allows the creationDate strings to just be compared lexicographically

Related

Get column (field) names from javascript Array [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 6 months ago.
I am looking to pull the list of column(field) names from this array. So that I end up with an array containing ['EntityId', 'RiskDescription', 'ThirdColumn', 'FourthColumn'].
var risks = [];
risks.push({
EntityId: this.EntityID
RiskDescription: this.RiskDescription
ThirdColumn: this.ThirdColumn,
FourthColumn: this.FourthColumn
});
You can use Object.keys to get the list of keys in that object
var risks = [];
risks.push({
EntityId: "value",
RiskDescription: "value",
ThirdColumn: "value",
FourthColumn: "value"
});
console.log(Object.keys(risks[0]))

JavaScript Array: Order Nested Object by Key [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
How to sort strings in JavaScript
(16 answers)
Sort Array by ISO 8601 date
(8 answers)
Closed last year.
I have the following nested object. I would like to iterate through every item (latest.sentTime) and then sort the object itself by "sentTime". So that the most recent message is on top. How can I achieve this?
In the following example, "0" and "1" need to be swapped basically, since "1" has a more recent "sentTime".
Using Array#map, iterate over the array to get latest.sentTime
Using Array#sort and Date, sort the items
const arr = [
{ latest: { sentTime: '2022-02-05T19:15:32.000Z' } },
{ latest: { sentTime: '2022-02-06T22:12:00.000Z' } }
];
const sentTimes = arr
.map(({ latest }) => latest.sentTime)
.sort((a, b) => new Date(b) - new Date(a));
console.log(sentTimes);

Convert array to object with key and value in javascript [duplicate]

This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 5 years ago.
How covert or add array elements into object.
Now
arr = [
["1/1/2017",113],
["3/11/2017",58],
["3/12/2017",70],
["3/13/2017",76]
]
Should be
arr = [
{date:"1/1/2017", count:113},
{date:"3/11/2017", count:58},
{date:"3/12/2017", count:70},
{date:"3/13/2017", count:76}
]
arr.map(function(arr1) {
return {
date : arr1[0],
count : arr1[1]
}
})

Sort array of array on string value in Javascript [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 6 years ago.
I want to sort the array of array on length of string, as follows
var arr = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
var sortedArr = sortOnLengthOfString(arr);
>> sortedArr = [
[1951, "Mayfer"],
[1101, "Cetaphil"],
[1007, "Ferri Injection"],
[1785, "Actinac Capsules"]
]
Is there a solution with lodash preferably? Or plain Javascript?
I haven't found duplicate question yet. Can someone find it? Please note, the sorting I am asking for, is for array of arrays and not array of objects.
You can use Array#sort at this context,
var obj = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
obj.sort(function(a,b){
return a[1].length - b[1].length
});
console.log(obj);
//[[1951, "Mayfer"],[1101, "Cetaphil"],[1007, "Ferri Injection"],[1785, "Actinac Capsules"]]

How do I sort an array filled with objects, based on two values of the object? [duplicate]

This question already has answers here:
Sort by two values prioritizing on one of them
(8 answers)
Closed 8 years ago.
I've got an array populated with objects. These objects start with two values: sort1 and sort2.
var arr_obj = [
{
sort1:'3000',
sort2:'200',
value:'Value1'
},
{
sort1:'4000',
sort2:'100',
value:'Value2'
},
{
sort1:'6000',
sort2:'200',
value:'Value3'
},
{
sort1:'6000',
sort2:'100',
value:'Value4'
},
{
sort1:'2000',
sort2:'500',
value:'Value5'
}
];
How do I go about sorting the objects based on the values of sort1 and sort2. Sort1 is dominant and if two values have the same sort1 value then the sort function should look at sort2 to determine which one comes first.
So for my example array the output should become.
[
{
sort1:'2000',
sort2:'500',
value:'Value5'
},
{
sort1:'3000',
sort2:'200',
value:'Value1'
},
{
sort1:'4000',
sort2:'100',
value:'Value2'
},
{
sort1:'6000',
sort2:'100',
value:'Value4'
},
{
sort1:'6000',
sort2:'200',
value:'Value3'
}
];
I tried just using a simple sort() on the array, but this probably looks at the index not the first values.
Does anyone know how to get the requested output?
FIDDLE
You can set a compare function into the sort.
arr_obj.sort(function(a,b) {
if (a.sort1 === b.sort1) {
return a.sort2 - b.sort2;
}
return a.sort1 - b.sort1;
});
Try this:-
arr_obj.sort(function(prev,next){
if(prev.sort1 != next.sort1){
return prev.sort1 > next.sort1;
}else{
return prev.sort2 > next.sort2;
}
});

Categories

Resources