An object comes in which there are properties. How can you sort an object alphabetically by the first letter of the properties? Is it possible to implement this on jQuery?

Sample object:

obj = { { name: "banan" }, { name: "ananas" }, { name: "grusha" } } 
  • 2
    You probably meant the array obj = [ ... ] ? - kmv
  • @ kmv, yes array - Dementiy1999
  • one
    learn.javascript.ru/task/sort-objects here is almost your task, you only need to change the mechanics of sorting

3 answers 3

Option with lodash

 var obj = [{ name: "banan" }, { name: "ananas" }, { name: "grusha" }] var result = _.sortBy(obj, function(o) { return o.name.charAt(0); }); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.min.js"></script> 

     obj.sort(function(a, b) { if(a.name[0] < b.name[0]) return -1; if(a.name[0] > b.name[0]) return 1; return 0; }) 
    • enough obj.sort (function (a, b) {return a.name [0]> b.name [0]}) - Jean-Claude

    Option with AngularJS

     <span ng-repeat="list in YourControllerAlias.obj | orderBy: 'name'">list.name</span>