JavascriptObjectSort

by michaelschmid

HTML

<h1>Sorting Object Attributes by proprty name</h1>

<h2>Un-Sorted</h2>
<div id="unsorted"></div>
<h2>Sorted</h2>
<div id="sorted"></div>

JavaScript

// display the sorted attributes for getting better pciture
									
var data = {
  "f5":"value5", 
  "f4":"value4", 
  "f3":"value3", 
  "f2":"value2", 
  "f1":"value1", 
};
var sorted = {};

document.getElementById('unsorted').innerHTML = JSON.stringify(data);

// sort attributes of an object
Object.keys(data).sort().map(function (key) {
  sorted[key] = data[key];
  console.log(key);
});
                  
document.getElementById('sorted').innerHTML = JSON.stringify(sorted);