Reputation: 117
I'm unable to sort an array containing int. Here's my code for the sort :
var asr = [];
function sortNumber(a, b) {
return b - a;
}
asr.sort(sortNumber);
The function sort
doesn't do anything, here is a sample of my array when I made a console.log(asr)
on chrome :
by: 3
de: 2
ds: 14
sr: 2
vi: 1proto: Array(0)
Upvotes: 0
Views: 92
Reputation: 652
Properties in arrays remain in the order that you define them, if you want to have them sorted then you need to define them sorted from the beginning, that being said you can get all the properties of your array, sort the values for this properties from your array and insert this sorted properties into a new array.
const asr = [];
asr.by = 3;
asr.de = 2;
asr.ds = 14;
asr.sr = 2;
asr.vi = 1;
function sortNumber(a, b) {
return b - a;
}
let asr2 = [...asr];
Object.keys(asr)
.map(key => ({ key, value: asr[key] }))
.sort((a, b) => b.value - a.value)
.forEach(a => asr2[a.key] = a.value);
console.log(asr2)
/*
// if asr contains values and you also
// want to sort those values you need
// to sort them separately
asr2 = asr2.sort((a, b) => b - a)
console.log(asr2)
*/
If what you what is to keep the properties in the same position but sort only the values then try this
const asr = [];
asr.by = 3;
asr.de = 2;
asr.ds = 14;
asr.sr = 2;
asr.vi = 1;
function sortNumber(a, b) {
return b - a;
}
const asrKeys = Object.keys(asr);
const asrSortedValues = asrKeys.map(key => asr[key])
.sort((a, b) => b - a);
asrKeys.forEach((key, index) => asr[key] = asrSortedValues[index]);
console.log(asr)
Upvotes: 0
Reputation: 2766
Based on your recent update, it seems asr
is an array with properties you've added. That's not generally a good idea unless you know what you're doing, and sort()
won't touch those properties.
Instead, I would use a normal object, with the caveat that objects in JavaScript aren't really meant to contain an ordered collection of values. With that caveat out of the way, this is how I'd store the data, and how I'd sort the keys:
const asr = {by: 3, ds: 14, de: 2, vi: 1, sr: 2}
console.log(
Object.fromEntries(
Object.entries(asr).sort(
([ak, av], [bk, bv]) => av > bv ? 1 : -1
)
)
)
I'll keep the rest here, even though it isn't relevant to your question.
asr
is most likely an array of objects, in which case asr.sort()
has sorted the array by the result of the contained objects' toString
method:
const asr = [{vi: 1}, {sr: 2}, {by: 3}, {ds: 14}, {de: 2}]
console.log(asr.sort())
If you want to sort it by object values, this will do the trick:
const asr = [{vi: 1}, {sr: 2}, {by: 3}, {ds: 14}, {de: 2}]
console.log(asr.sort(
(a, b) => Object.values(a)[0] > Object.values(b)[0] ? 1 : -1
))
If you want to sort by object keys, this should work:
const asr = [{vi: 1}, {sr: 2}, {by: 3}, {ds: 14}, {de: 2}]
console.log(asr.sort(
(a, b) => Object.keys(a)[0] > Object.keys(b)[0] ? 1 : -1
))
Upvotes: 3
Reputation: 957
Your sort function is correct, check with: console.log([51,2,13,4,5].sort(function(a,b) {return b-a}));
asr
doesn't seem to be an array but an object.
Upvotes: 0