Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

npm package for sorting in reactjs

var sortBy = require('sort-by'),
    users = [];

users = [{
    id: 7,
    name: 'Foo',
    age: '34',
    email: { primary: 'foo@email.com' }
}, {
    id: 3,
    name: 'Baz',
    age: '67',
    email: { primary: 'baz@email.com' }
}, {
    id: 4,
    name: 'Bar',
    age: '67',
    email: { primary: 'bar@email.com' }
}];

users.sort(sortBy('name', 'age'));

/**
*   result:
*       [{id: 4, name: 'Bar', age: '67', email: { primary: 'bar@email.com' }},
*       {id: 3, name: 'Baz', age: '67', email: { primary: 'baz@email.com' }},
*       {id: 7, name: 'Foo', age: '34', email: { primary: 'foo@email.com' }}]
*/

/**
* Use `-` to reverse the sort order
*/

users.sort(sortBy('-id', 'name'));

/*
*   result:
*       [{id: 7, name: 'Foo', age: '34', email: { primary: 'foo@email.com' }},
*       {id: 4, name: 'Bar', age: '67', email: { primary: 'bar@email.com' }},
*       {id: 3, name: 'Baz', age: '67', email: { primary: 'baz@email.com' }}]
*/

/**
* Use `.` notation to traverse nested properties. See [object-path](https://www.npmjs.org/package/object-path) npm module for support.
*/

users.sort(sortBy('age', 'email.primary'));

/*
*   result:
*       [{id: 7, name: 'Foo', age: '34', email: { primary: 'foo@email.com' }},
*       {id: 4, name: 'Bar', age: '67', email: { primary: 'bar@email.com' }},
*       {id: 3, name: 'Baz', age: '67', email: { primary: 'baz@email.com' }}]
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: instantiate object in script godot 
Javascript :: how to stop iframe video using javascript 
Javascript :: uuid javascript 
Javascript :: regex for 4 digit number javascript 
Javascript :: react native flatlist from bottom to top 
Javascript :: node js request download file 
Javascript :: javascript check date greater than today 
Javascript :: react native portrait only 
Javascript :: how to format multiline string in javascript 
Javascript :: text and icon on same line react native 
Javascript :: angular display block 
Javascript :: html javascript redirect 
Javascript :: redirect the page in javascript 
Javascript :: vehicle number formik validation 
Javascript :: array.foreach 
Javascript :: cypress set viewport for all test cases 
Javascript :: how to return character associated to character code javascript 
Javascript :: jquery get document scrolltop 
Javascript :: document selector query change value 
Javascript :: html canvas draw base64 image 
Javascript :: get input value in react using hooks 
Javascript :: credit card mask js 
Javascript :: array unique values javascript 
Javascript :: how to get file size in node js 
Javascript :: express get ip address of request 
Javascript :: noise margin in digital electronics 
Javascript :: pagination jsonplaceholder 
Javascript :: js export as name 
Javascript :: angular local storage 
Javascript :: next router push state 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =