There is an array of objects, you need to sort it by field value, placing elements with the same value next to each other. Example:

var arr1 = [ {str:'strA'}, {str:'bStr'}, {str:'Cstr'}, {str:'strA'}, {str:'bstr'}; ]; //исходный массив var arr1 = [ {str:'strA'}, {str:'strA'}, {str:'bStr'}, {str:'bstr'}, {str:'Cstr'}; ]; //исходный массив 

In the "arms" there are knockout.js and underscore.js .

  • You have a strange sequence: why is 9 before 3? - user207618
  • This is an example. What interests you is not an increasing sequence of numbers, but sorting in such a way that the same values ​​are next to each other in the array. - NoireHouse

1 answer 1

The array sort () method will help you.

 arr = [ {str: 1}, {str: 2}, {str: 3}, ]; arr.sort(function(a, b) { return a.str > b.str }); 

If you want to leave the original array unsorted, then you can use the slice () method to copy

 arr2 = arr1.slice() 
  • My bad. parameter by which is sorted: string. - NoireHouse
  • Corrected the answer to your new example. - Sergey Gornostaev