0

I have an array in javascript called 'environment' .

I wish to sort this array based on the length of the ids of the elements . I am doing this as follows :

   for (i = 0; i < environment.length; i++) { 
    console.log(environment[i]['id']);
    // prints dev,production,staging ... I want it to be dev,staging,production
}

How can I achieve this ? TIA :)

ggwp
  • 77
  • 1
  • 9
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – PM 77-1 Feb 27 '15 at 05:13
  • Try implementing quicksort as an exercise: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ – mjwunderlich Feb 27 '15 at 14:14

2 Answers2

3
environment.sort(function(a, b){return a.id.length > b.id.length})
Secret
  • 3,291
  • 3
  • 32
  • 50
0

You can use environment.sort().

Daniel Morris
  • 6,852
  • 8
  • 25
  • 30