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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -