sort an array of object accoring to the values

222
August 17, 2019, at 07:00 AM

I have this array of objects:

const array =[ 
  { a: 'good car' },
  { a: 'good car1' },
  { a: 'good car2' },
  { a: 'good car3' },
  { a: 'good car4' },
  { b: 'good car1' } 
];

I need to sort it via values except in the case of ties. In the case of ties, the key is used to break the tie. I searched A LOT but couldn't use the answers as my keys in objects are not the same and also in case of the tie (values are the same), I cannot handle that. Any suggestions?

Answer 1

You could get the entries, pick the first one and destructure it to key and value and take the same for the other object, then return the chained sorting values.

var array = [{ v21: 'sad sdd' }, { aaa: 'sad sdd' }, { v11: 'r err rr' }, { hf32: 'erwwer fgh' }, { z3f2: 'agfrr vbbar' }, { t142: 'gggoog  anfa' }, { u23: 'fa handle err' }]; 
 
array.sort((a, b) => { 
    var [keyA, valueA] = Object.entries(a)[0], 
        [keyB, valueB] = Object.entries(b)[0]; 
         
    return valueA.localeCompare(valueB) || keyA.localeCompare(keyB); 
}); 
 
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer 2

use sort from es6.

try this code:

const array =[ 
  { v21: 'sad sdd' },
  { v11: 'r err rr' },
  { hf32: 'erwwer fgh' },
  { z3f2: 'agfrr vbbar' },
  { t142: 'agfrr vbbar' },
  { u23: 'fa handle err' } 
]
array.sort( (x,y) => { 
  if ( Object.values(x)[0] > Object.values(y)[0] )
    return 1
  else if ( Object.values(x)[0] < Object.values(y)[0] )
    return -1
  else {
    if ( Object.keys(x)[0] > Object.keys(y)[0] )
      return 1
    else if ( Object.keys(x)[0] < Object.keys(y)[0] )
      return -1
  }
})
Answer 3

You can try the following code:

const array =[  
  { v21: 'sad sdd' }, 
  { v11: 'r err rr' }, 
  { hf32: 'erwwer fgh' }, 
  { z3f2: 'agfrr vbbar' }, 
  { z3f1: 'agfrr vbbar' }, 
  { t142: 'gggoog  anfa' }, 
  { u23: 'fa handle err' }  
]; 
array.sort((a,b) => { 
      if (a[Object.keys(a)[0]] === b[Object.keys(b)[0]]) { 
          return Object.keys(a)[0].localeCompare(Object.keys(b)[0]); 
      } 
      return a[Object.keys(a)[0]].localeCompare(b[Object.keys(b)[0]]); 
}); 
console.log(array);

Answer 4

A variant without using compare function:

const array = [
  { v21: "sad sdd" },
  { v11: "r err rr" },
  { hf32: "erwwer fgh" },
  { z3f2: "sad sdd" },
  { t142: "gggoog  anfa" },
  { u23: "fa handle err" }
];
const result = array
  .map(item => ({
    key: Object.keys(item)[0],
    value: item[Object.keys(item)[0]]
  }))
  .map(item => `${item.value}:${item.key}`)
  .sort()
  .map(item => item.split(":"))
  .map(item => ({ [item[1]]: item[0] }));
console.log(result);

have to use maybe some other join char instead of ':' if it is expected in the value

Answer 5

Thanks for all of your answers, but I finally solved it myself :) the interesting thing is that my solution is very similar to atmrts' solution, so with thanks to everyone I think I should select that one as we solved it kinda in the same way.

Rent Charter Buses Company
READ ALSO
How can I reference an external javascriptfile in angular?

How can I reference an external javascriptfile in angular?

I am trying to create a sliding menu for my aplication in angular, I would like to reference a javascript fileI have referenced css files in the angular

147
How to properly use a ternary expression with @click event in Vue.js

How to properly use a ternary expression with @click event in Vue.js

I'm showing a list of release versions, but the part I'm stuck on is I want to be able to click the release version and show the job_execs for that versionI'm not sure how to do this other than using a ternary expression and binding it to click event

435
Is it possible to have Testcafe remember an h1 or a text from a previous page so I can match/compare it in another page?

Is it possible to have Testcafe remember an h1 or a text from a previous page so I can match/compare it in another page?

I am trying to do a smoke test on a web page and essentially if I can somehow get Testcafe to remember "Hello World" from page 1 and go to a User Profile to match that same element with the text "Hello World" then I can make it work for all the other cases I will have

144
How to send different AJAX URL if input is empty

How to send different AJAX URL if input is empty

I'm making a live search with AJAX that searches all the rows of the SQL column called number and sorts by numberBut I cant figure out how to change the SQL sort to another column called wt when the input is empty

123