This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
I have an array of objects like the one below. Although, the order isn't in ascending or descending. Is there function that sorts the numbers in each object and replacing it into a new array or something similar?
let array = [
{
number: 16
},
{
number: 25
},
{
number: 20
},
{
number: 28
}
];
Yes, you can use array.sort() on a copy of your array.
example:
let newArray = [...array].sort((a, b) => a.number - b.number)
That will sort your array ascending.
If you want it descending just switch to b.number - a.number
More info on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Related
This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Closed 6 months ago.
Given the array of numbers
let el = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000];
The output of the following suggests that it ignores the value of 3000
let el = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000];
console.log(el.sort());
//Output [10000, 25000, 29000,3000, 33000, 40000,42000, 48000, 54000,57000, 79000, 86000]
So it did sort, but didn't move the 3000 at the 4th position. Why is this happening?
As VLAZ explained, By default, the sort() function sorts values as strings. if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce incorrect results when sorting numbers.
You can fix this by providing a compare function: (a, b) => a - b)
compareFn(a, b) return value
sort order
> 0
sort a after b
< 0
sort a before b
=== 0
keep original order of a and b
source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description
e.g.
let el = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000];
console.log(el.sort((a, b) => a - b));
This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
How to sort multidimensional array in javascript
(4 answers)
How to sort an object array by date property?
(25 answers)
Closed 1 year ago.
I have an array for a timeline chart of Google Charts
So the array looks like follows
let data = [
['Outreach ICU Follow-up', '2021-01-25', 2021-02-01],
['Hospital Stay' , '2021-01-01', '2021-03-01'],
['ICU Stay' , '2021-01-02', '2021-01-25'],
['Outreach ICU Follow-up', '2021-02-20', '2021-03-01'],
['ICU Stay' , '2021-02-01', '2021-02-20'],
]
I lined up the date to see the chronological order. I need to sort this by the first data ( element 1 )
Cant use this because its for objects I think.
let ass = data.sort((x,y)=>y.dlk_assess_order-x.dlk_assess_order);
And this one is futile as-well.
var data = data.sort(function(a, b) { return a - b; });
How does one sort and array like this please?
Is there perhaps a lodash function?
As your question said, you simply sort by the element at position 1
let data = [
['Outreach ICU Follow-up', '2021-01-25', '2021-02-01'],
['Hospital Stay' , '2021-01-01', '2021-03-01'],
['ICU Stay' , '2021-01-02', '2021-01-25'],
['Outreach ICU Follow-up', '2021-02-20', '2021-03-01'],
['ICU Stay' , '2021-02-01', '2021-02-20'],
]
const sorted = data.sort((a,b) => a[1] - b[1]);
console.log(sorted);
This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
Closed 2 years ago.
I have an array like this:
let pairs = [[2,"test"], [7,"buzz"], [3,"asd"]]
How can i sort the outer array by the first values? (descending)
Pass a compare function to Array.sort that examines the first element in each array, to determine the required order.
> let pairs = [[2,"test"], [7,"buzz"], [3,"asd"]]
> pairs.sort((a, b) => a[0] - b[0]);
[ [ 2, 'test' ], [ 3, 'asd' ], [ 7, 'buzz' ] ]
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings into an array of objects
(6 answers)
Closed 3 years ago.
I want to convert an Array like:
[ 'John', 'Jane' ]
into an array of object pairs like this:
[{'name': 'John'}, {'name':'Jane'}]
Please help me to do so..
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);
This question already has answers here:
Sort Array Elements (string with numbers), natural sort
(8 answers)
Closed 5 years ago.
We have a bunch of SomeItems which have a field someId which is in the format (simplified / replaced with dummies):
abc.def1.<someNumber>
I have an array of SomeItems and have been sorting it like this:
let rows = someItems.sort((lhs, rhs) => lhs.someId > rhs.someId)
This pretty much just sorts it alphabetically, so this means that the order comes out a little weird:
abc.def1.1
abc.def1.1234
abc.def1.1235
abc.def1.2
abc.def1.234
I instead want it to be numerically sorted - like 1, 2, 234, 1234, etc.
What's a clean way to do this?
You could use String#localeCompare with options.
var array = ['abc.def1.1', 'abc.def1.1234', 'abc.def1.1235', 'abc.def1.2', 'abc.def1.234'];
array.sort(function (a,b) {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(array);