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
  • 20,093
  • 6
  • 21
  • 40
Diamonds
  • 117
  • 1
  • 9

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,756
  • 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
  • 929
  • 5
  • 12
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)
  • 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