0

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: 1

proto: Array(0)

11
  • 5
    You don't have an array, you have an object. You can't sort objects. Commented Jan 9, 2020 at 21:51
  • Can you post the original array - if that's an array - called asr what you have in the code? Thanks! Commented Jan 9, 2020 at 21:51
  • @Pointy Why does console.log(asr.constructor.name == "Array"); return me true then ? Commented Jan 9, 2020 at 21:55
  • 2
    My guess is that you take an array, but then you attach properties to it as an object (arrays in JS are objects as well). Something like var asr = []; asr.by = 3; asr.de = 2; // etc... Commented Jan 9, 2020 at 21:57
  • 2
    Arrays in JS are also objects, and you can freely attach values to string keys. But the sort method won't touch those, it will just affect the integer keys ("indices"). Commented Jan 9, 2020 at 22:00

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks and what If I got object of objects instead of array of objects for sorting by object values ?
@Diamonds It depends on what you want the final result to look like. Do you want to end up with an array of objects? An object of objects?
Object of objects because I need the keys to identify each part
@Diamonds I've added another snippet and some thoughts at the top of this answer.
Thanks but it doesn't work with all of my datas while It has the same format as the example..
|
0

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.

Comments

0

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)

2 Comments

With my full data everything is fine, data are sorted well, but when it comes to the .forEach asr2 is the same as asr
This is because foreach (and other and other Array.prototype methods like map, reduce and sort) will ignore your added properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.