0

Say I have an array:

var array = [ { first: 'qwe123', second: 1234 }, {..}, .. ];

I want to sort an array by second keys value(1234) in descending order and put all elements' values into one string. Is it possible to do so?

madim
  • 774
  • 10
  • 22

1 Answers1

0

You may use:

The example:

var array = [ {first: 'qwe123', second: 1}, 
             {first: 'qwe123', second: 2}, 
             {first: 'qwe123', second: 3}, 
             {first: 'qwe123', second: 4}];
             
var result = array.sort(function(a, b) {
       return +b.second -  +a.second;
   }).map(function(ele, idx) {
       return ele.second;
   }).join(', ');
             
console.log(result);
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
  • i changed it little to return the value of both first and second, anyway thank you very much – madim Sep 16 '16 at 16:50