javascript - ReactJS: How do I sort an array of objects based on value of props? -


i have array of react objects , want sort them based on value of 1 of props.

var arr=[]; arr[0] = <fruit name="orange" count={10}/> arr[1] = <fruit name"apple" count={5}/> 

is there built in react function can use sort array in ascending order of fruit count?

react not have built in function handle (that know of), use lodash achieve want. suggest change structure of data slightly. have @ example below.

// data comes in   var arr = [     {name: "orange", count: 10},      {name: "apple", count: 5},     {name: "lemon", count: 11},     {name: "grape", count: 2} ];  // order ascending var newarr = _.sortby(arr, 'count', function(n) {   return math.sin(n); });  // create components var fruits = newarr.map(function(fruit) {    return(       <fruit name={fruit.name} count={fruit.count} />    ); }); 

here fiddle illustrate sorting function input , output


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -