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)

Mister Jojo
  • 16,804
  • 3
  • 16
  • 39
Diamonds
  • 117
  • 8
  • 5
    You don't have an array, you have an object. You can't sort objects. – Pointy Jan 09 '20 at 21:51
  • Can you post the original array - if that's an array - called `asr` what you have in the code? Thanks! – norbitrial Jan 09 '20 at 21:51
  • @Pointy Why does `console.log(asr.constructor.name == "Array");` return me `true` then ? – Diamonds Jan 09 '20 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...` – Ori Drori Jan 09 '20 at 21:57
  • @norbitrial I'm using value from poperties of objects to fill it but at first I declare it like an array : `var asr = [];` – Diamonds Jan 09 '20 at 21:58
  • you've been answered. it's an object. try to sort Object.values(arr) – dandavis Jan 09 '20 at 21:59
  • Please show how you define and initialize `asr`. – Code-Apprentice Jan 09 '20 at 22:00
  • @OriDrori Yeah I think that I have been confused because we can also call object properties like an array i.e `asr[by]` – Diamonds Jan 09 '20 at 22:00
  • 1
    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"). – Robin Zigmond Jan 09 '20 at 22:00
  • this is because your array is empty – Mister Jojo Jan 09 '20 at 22:04
  • Does this answer your question? [How to sort an array of integers correctly](https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly) – MSallal Jan 09 '20 at 23:20

3 Answers3

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
))
tex
  • 2,716
  • 22
  • 31
  • Thanks and what If I got object of objects instead of array of objects for sorting by object values ? – Diamonds Jan 09 '20 at 22:25
  • @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? – tex Jan 09 '20 at 22:28
  • Object of objects because I need the keys to identify each part – Diamonds Jan 09 '20 at 22:30
  • @Diamonds I've added another snippet and some thoughts at the top of this answer. – tex Jan 09 '20 at 22:40
  • Thanks but it doesn't work with all of my datas while It has the same format as the example.. – Diamonds Jan 09 '20 at 22:51
  • The format you have is suspect. It seems you may be confused about how arrays and their methods work. Array methods work on the contents of an array, like `[4,3,2,1].sort() // -> [1,2,3,4]`. Those methods (e.g. `forEach` and `sort`) will ignore any properties you have added to the array. – tex Jan 09 '20 at 22:56
  • Yeah I know I have corrected my code by using only objects with no confusion with array but still.. – Diamonds Jan 09 '20 at 22:57
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.

mottek
  • 775
  • 4
  • 12
0

Properties in arrays remain in the order that you define them, if you what 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;

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)

/*
  ds: 14
  by: 3
  de: 2
  sr: 2
  vi: 1
*/

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

asrKeys.forEach((key, index) => asr[key] = asrSortedValues[index]);

console.log(asr)

/*
    by: 14
    de: 3
    ds: 2
    sr: 2
    vi: 1
*/
  • With my full data everything is fine, data are sorted well, but when it comes to the `.forEach` `asr2` is the same as `asr` – Diamonds Jan 09 '20 at 22:49
  • This is because `foreach` (and other and other `Array.prototype` methods like `map`, `reduce` and `sort`) will ignore your added properties. – tex Jan 09 '20 at 22:54