Sort array by values in subarrays [duplicate] - javascript

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' ] ]

Related

Sort array of arrays ( could not find duplicate for THIS instance ) [duplicate]

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);

Sorting numbers in array of objects [duplicate]

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

Sort array of objects based on the ordered list of values [duplicate]

This question already has answers here:
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
const arr = [
{Id:"3",name: "ADMIN"},
{Id:"1",name: "SECURITY"},
{Id:"2",name: "INFORMATION_REPORTING"},
{Id: "23",name: "PAYMENTS_SERVICES"},
{Id: "344",name: "PAYMENT_HUB"},
{Id: "31",name: "RTP"},
{Id: "43",name: "PAYMENTS"},
{Id: "34",name: "GPI_ALERTS"},
{Id: "65",name: "ADMINISTRATION"}
]
I have the arr which has the values as describing here.And I want to reorder the arr using the key name as below, Order to be shown.
ADMIN
ADMINISTRATION
PAYMENTS
RTP
PAYMENTS_SERVICES
INFORMATION_REPORTING
PAYMENT_HUB
SECURITY
GPI_ALERTS
So I want the arr in this order shown above based on name key.
You may use Array.prototype.sort() and compare arr items based on their position (Array.prototype.indexOf()) within orderList
const arr = [{Id:"3",name:"ADMIN"},{Id:"1",name:"SECURITY"},{Id:"2",name:"INFORMATION_REPORTING"},{Id:"23",name:"PAYMENTS_SERVICES"},{Id:"344",name:"PAYMENT_HUB"},{Id:"31",name:"RTP"},{Id:"43",name:"PAYMENTS"},{Id:"34",name:"GPI_ALERTS"},{Id:"65",name:"ADMINISTRATION"}],
orderList = ['ADMIN','ADMINISTRATION','PAYMENTS','RTP','PAYMENTS_SERVICES','INFORMATION_REPORTING','PAYMENT_HUB','SECURITY','GPI_ALERTS'],
result = arr.sort(({name:nameA},{name:nameB}) =>
!orderList.includes(nameA) ?
1 :
!orderList.includes(nameB) ?
-1 :
orderList.indexOf(nameA) - orderList.indexOf(nameB)
)
console.log(result)
.as-console-wrapper{min-height:100%;}

How to convert an Array to Array of Object in Javascript [duplicate]

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);

Javascript sort by element inside array [duplicate]

This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
Closed 6 years ago.
I need to sort an complex array order by one column of the array.
For example, this array might looks like
array = [["Banana","Chapter3"], ["Orange","Chapter2"], ["Apple","Chapter1"]];
I want it to sort by Chapter, so the result will be
array = ["Apple","Chapter1"],["Orange","Chapter2"],["Banana","Chapter3"]]
But if I do array.sort, it will become
[["Apple","Chapter1"],["Banana","Chapter3"],["Orange","Chapter2"]]
It seems sort by first element's ascii code. How do I sort by specific element in array?
I also created a JSfiddle to illustrate my idea.
You need to pass a comparator to sort:
var array = [["Banana","Chapter3"], ["Orange","Chapter2"], ["Apple","Chapter1"]];
var sorted = array.sort(function (a, b) {
if (a[1] > b[1]) {
return 1;
} else if (a[1] < b[1]) {
return -1;
} else {
return 0;
}
});
console.log(sorted);
// [ [ 'Apple', 'Chapter1' ],
// [ 'Orange', 'Chapter2' ],
// [ 'Banana', 'Chapter3' ] ]

Categories

Resources