Sorting a JavaScript object by property name

javascript sort object by key
javascript sort object by value
javascript sort array of objects by key
javascript sort array of objects by key value
javascript sort array of objects by multiple properties
javascript sort array of objects by number
angular 4 sort array of objects by property
javascript sort by field name

I've been looking for a while and want a way to sort a Javascript object like this:

    method: 'artist.getInfo',
    artist: 'Green Day',
    format: 'json',
    api_key: 'fa3af76b9396d0091c9c41ebe3c63716'

and sort is alphabetically by name to get:

    api_key: 'fa3af76b9396d0091c9c41ebe3c63716',
    artist: 'Green Day',
    format: 'json',
    method: 'artist.getInfo'

I can't find any code that will do this. Can anyone give me some help?

By definition, the order of keys in an object is undefined, so you probably won't be able to do that in a way that is future-proof. Instead, you should think about sorting these keys when the object is actually being displayed to the user. Whatever sort order it uses internally doesn't really matter anyway.

By convention, most browsers will retain the order of keys in an object in the order that they were added. So, you could do this, but don't expect it to always work:

function sortObject(o) {
    var sorted = {},
    key, a = [];

    for (key in o) {
        if (o.hasOwnProperty(key)) {


    for (key = 0; key < a.length; key++) {
        sorted[a[key]] = o[a[key]];
    return sorted;

How to sort an array of objects by a property value in JavaScript, You want to render this list, but first you want to order it by the value of one of the properties. For Duration: 4:07 Posted: Dec 6, 2018 When we return 1, the function communicates to sort() that the object b takes precedence in sorting over the object a. Returning -1 would do the opposite. The callback function could calculate other properties too, to handle the case where the color is the same, and order by a secondary property as well:

this function takes an object and returns a sorted array of arrays of the form [key,value]

function (o) {
   var a = [],i;
   for(i in o){ 
   a.sort(function(a,b){ return a[0]>b[0]?1:-1; })
   return a;

The object data structure does not have a well defined order. In mathematical terms, the collection of keys in an object are an Unordered Set, and should be treated as such. If you want to define order, you SHOULD use an array, because an array having an order is an assumption you can rely on. An object having some kind of order is something that is left to the whims of the implementation.

Sorting an array of objects, The sort() method can be used to sort the array based on the values of one of these properties, such as sorting the array by name, age, or even when they retire. The basic idea is to modify the compare function so it compares the desired properties' values. Sorting an array of objects. Taking things one step further, lets say your array doesn't contain just simple numeric or string values, but objects with properties instead: The employees array above is an array of objects with properties of different data types, from string, numeric, to a date string.

Just use sorted stringify() when you need to compare or hash the results.

Quick Tip: How to Sort an Array of Objects in JavaScript, If you have an array of objects that you need to sort into a certain order, you By default, the JavaScript Array.sort function converts each element in the const singers = [ { name: 'Steven Tyler', band: 'Aerosmith', born: 1948 }  Call sort on the array, passing in a comparator function In the comparator function, put "sort_" in front of each of the two values the function was given to compare, and use that property name to look up the the numeric string for that property in the object Parse those two numeric strings into numbers

// if ya need old browser support
Object.keys = Object.keys || function(o) {  
var result = [];  
for(var name in o) {  
    if (o.hasOwnProperty(name))  
    return result;  

var o = {c: 3, a: 1, b: 2};
var n = sortem(o);

function sortem(old){
  var newo = {}; Object.keys(old).sort().forEach(function(k) {new[k]=old[k]});
  return newo;

// deep
function sortem(old){
  var newo = {}; Object.keys(old).sort().forEach(function(k){ newo[k]=sortem(old[k]) });
  return newo;

sort object by property - JSFiddle, Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code​  underscore.js. use underscore, its small and awesome sortBy_.sortBy(list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator.

This should be used with caution as your code shouldn't rely on Object properties order. If it's just a matter of presentation (or just for the fun !), you can sort properties deeply like this :

function sortObject(src) {
  var out;
  if (typeof src === 'object' && Object.keys(src).length > 0) {
    out = {};
    Object.keys(src).sort().forEach(function (key) {
      out[key] = sortObject(src[key]);
    return out;
  return src;

JavaScript Array sort: Sorting an Array More Effectively, This tutorial shows you how to use the JavaScript array sort method to sort arrays of numbers, arrays of strings, and arrays of objects by a property. employee objects, where each object contains three properties: name , salary and hireDate . The data which you want to sort; The property in the data by it should be sorted; The last parameter is of boolean type. It checks if you want to sort by ascending or by descending

Array.prototype.sort(), Objects can be sorted, given the value of one of their properties. var items = [ { name: 'Edward', value: 21 }, { name: 'Sharpe', value: 37 }  Sort an Array of Objects by Property Using sort(fn) Believe it or not, there's more to JavaScript than the helper methods and classes they provide your JavaScript library. No, like seriously bro; these JavaScript-given methods do exist. One of those methods, sort, is provided to every Array instance via its prototype.

Sorting multiple object properties with JavaScript, Sorting regular arrays in javascript is a piece of cake - even sorting by object objects we can use the same logic, just by modifying the sort function slightly: a look at these students and see if we can sort them by grade, then by last name. The more work a compareFunction does and the more elements there are to sort, it may be more efficient to use map for sorting. The idea is to traverse the array once to extract the actual values used for sorting into a temporary array, sort the temporary array, and then traverse the temporary array to achieve the right order.

JavaScript Array Sort, JavaScript Sorting Arrays You can use it to sort an array in descending order: Even if objects have properties of different data types, the sort() method can be  The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties are not included in a command, PowerShell uses default sort properties. You can sort objects by a single property or multiple properties.

  • - Decent tutorial on sorting complex JS objects. - Another example --- Here is a full reference on the sort function if you want the details… -Last edit, I swear!
  • The problem I've got is that those articles show how to sort an array of objects. I'm trying to sort an object filled with objects. Will the same examples work for my case, if so could you provide an example? Sorry I'm just really stuck on this one :s
  • This is an ancient question, but I'm a pedant, so for the billionth time: there is no such thing as a JSON object--at least, not in the way you're referring to it. There are javascript objects and JSON strings.
  • Upvoted for the artist value. :D
  • There is better answer for ES6 at…
  • That seems to work perfectly. I'll have to keep an eye on it breaking in future browsers but it seems to work for now. Thanks for the help :)
  • It may in fact always work, and it is being codified into the Ecmascript standard, but it's still not a safe assumption to rely on, because having a defined order is not logically part of an "object". If you want to define order, you should use an array.
  • The only reason I need to do it is to sort the values once so I can generate an api signature as defined on this page in section 6: If I was using it for more than that I'd use an array and them sort that.
  • You should most definately use an array for that.
  • 'if (o.hasOwnProperty(key)) {' It's not necessary, also what about multi level objects, or arrays that contain objects or other way around? It is a good starting point though, mine was similar at first and then I upgraded it a bit...…
  • Quite nice and slick, but unfortunately Object.keys() is not supported in IE<9 and also not in Quirks mode of any IE>=9. See MSDN's IE Dev Center.
  • Added poly fill to above answer
  • If you mean deep sort of the obj, I edited the answer above. The 'new' was a typo.