Sorting JSON

Sorting JSON by a chosen key

by Andrew Pougher

HTML

<ul id="navlist"></ul>

CSS

li{border-bottom:1px solid #dddddd;padding:6px}

JavaScript

var myjson = [{"id":600385,"name":"Featured Titles","url":
    "http://www.example.com"},
  {"id":562680,"parentId":567182,"name":"Business","url":
    "http://www.example.com"},
  {"id":562677,"parentId":567186,"name":"Other Misc.","url":
    "http://www.example.com"},
  {"id":562664,"parentId":567186,"name":"News \u0026 History","url":
    "http://www.example.com"},
  {"id":562678,"parentId":567186,"name":"Men\u0027s General","url":
    "http://www.example.com"},
  {"id":562685,"parentId":567182,"thumbnailUrl":
    "http://www.example.com","name":
    "Animals","url":
    "http://www.example.com"},
  {"id":562679,"parentId":567189,"name":"Travel","url":
    "http://www.example.com"},
  {"id":562672,"parentId":567186,"name":"Outdoor Activities","url":
    "http://www.example.com"},
  {"id":562690,"parentId":567186,"name":"Music","url":
    "http://www.example.com"},
  {"id":562695,"parentId":567186,"name":"Military","url":
    "http://www.example.com"},
  {"id":562669,"parentId":567183,"name":"Entertainment","url":
    "http://www.example.com"},
  {"id":562688,"parentId":567182,"name":"Computers","url":
    "http://www.example.com"},
  {"id":562694,"parentId":567183,"name":"Electronics","url":
    "http://www.example.com"},
  {"id":562675,"parentId":567183,"name":"Games \u0026 Puzzles","url":
    "http://www.example.com"},
  {"id":562682,"parentId":567183,"name":"Fishing \u0026 Hunting","url":
    "http://www.example.com"},
  {"id":562681,"parentId":567183,"name":"Family","url":
    "http://www.example.com"}]

 
  myjson = myjson.sort(function(a,b) {
 return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
 });
 $.each(myjson,function( index, value ) {
 $('#navlist').append("<li>"+value.id+" "+value.name+" "+value.url+" </li>");
 });