sort object alphabetically javascript

85

sort object alphabetically javascript -

//Sort object by v1 property alphabetically (case insensitive)
let objArray= [
  {v1:"Bee", v2: 2}, 
  {v1:"Apple", v2: 8}, 
  {v1:"bat", v2: 4},
];
let sortedArray= objArray.sort(function(a, b) {
   return a.v1.localeCompare(b.v1);
}); // [{v1: "Apple", v2: 8}, {v1: "bat", v2: 2}, {v1: "Bee", v2: 2}]

sort object alphabetically javascript -

objArray.sort(function(a, b) {
    var textA = a.DepartmentName.toUpperCase();
    var textB = b.DepartmentName.toUpperCase();
    return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});

js sort object properties alphabetically -

const data = {
  States: ['NSW', 'VIC'],
  Countries: ['GBR', 'AUS'],
  Capitals: ['SYD', 'MEL']
}

const sortedObject = Object.fromEntries(Object.entries(data).sort())

Comments

Submit
0 Comments