[英]Sorting elements before rendering in React
I'm pretty new to JS and React and I wanted to sort aa list of elements alphabetically by the 'name.'我是 JS 和 React 的新手,我想按“名称”的字母顺序对元素列表进行排序。 I am going to put these elements into a component to be rendered, but I first want to make sure they are sorted the way I want.
我将把这些元素放入一个要渲染的组件中,但我首先要确保它们按照我想要的方式排序。 I'm familiar with Java and I wanted to know if there is something similar to comparators in JS that I could use to sort by 'name' for each element.
我熟悉 Java,我想知道是否有类似于 JS 中的比较器的东西,我可以用它来按每个元素的“名称”进行排序。
const [colleges, setColleges] = useState([
{name: 'Princeton \nUniversity', price: 1100.00, img: Princeton, tag: 'princeton', tag2: 'Princeton'},
{name: 'Stanford \nUniversity', price: 1100.00, img: Stanford, tag: 'stanford', tag2: 'Stanford'}},
{name: 'Brown \nUniversity', price: 1100.00, img: Brown, tag: 'brown', tag2: 'Brown'},
{name: 'Columbia \nUniversity', price: 1100.00, img: Columbia, tag: 'columbia', tag2: 'Columbia'},
{name: 'Wake Forest \nUniversity', price: 1100.00, img: Wake, tag: 'wake', tag2: 'wake forest'},
{name: 'Michigan, Ross', price: 1100.00, img: Ross, tag: 'ross', tag2: 'michigan'
}])
const [search, setSearch] = useState('')
const renderColleges = (colleges.sort()) => (colleges.map(colleges => <College {...colleges} />))
return (
<div>
<div className='title'>
<Input
className="input"
size="large"
placeholder="Search for a school"
onChange={(e) => setSearch(e.target.value)}
value={search}/>
</div>
<Row align="middle" className='container'>
{renderColleges(colleges.filter(c => c.name.includes(search) || c.tag.includes(search) || c.tag2.includes(search)))}
</Row>
</div>
);
}
const renderColleges = (colleges.sort((a,b) => {
return a.name.localeCompare(b.name)
}) => (colleges.map(colleges => <College {...colleges} />))
Switch the order of a and b if you want to sort by descending.如果要按降序排序,请交换 a 和 b 的顺序。 Always use localeCompare instead of > or < when sorting by string, it's much more accurate.
按字符串排序时,始终使用 localeCompare 而不是 > 或 <,这样更准确。 And flexible.
并且灵活。
Check this lib https://lodash.com/
检查这个库
https://lodash.com/
import { orderBy } from _ ;
const users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 34 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 36 }
];
// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
You can sort name in ascending order by using _.orderBy
您可以使用
_.orderBy
按升序对名称进行排序
I'm pretty new to JS and React and I wanted to sort aa list of elements alphabetically by the 'name.'我对 JS 和 React 还很陌生,我想按“名称”的字母顺序对元素列表进行排序。 I am going to put these elements into a component to be rendered, but I first want to make sure they are sorted the way I want.
我将把这些元素放入要呈现的组件中,但我首先要确保它们按我想要的方式排序。 I'm familiar with Java and I wanted to know if there is something similar to comparators in JS that I could use to sort by 'name' for each element.
我熟悉 Java,我想知道是否有类似于 JS 中的比较器的东西,我可以用它为每个元素按“名称”排序。
const [colleges, setColleges] = useState([
{name: 'Princeton \nUniversity', price: 1100.00, img: Princeton, tag: 'princeton', tag2: 'Princeton'},
{name: 'Stanford \nUniversity', price: 1100.00, img: Stanford, tag: 'stanford', tag2: 'Stanford'}},
{name: 'Brown \nUniversity', price: 1100.00, img: Brown, tag: 'brown', tag2: 'Brown'},
{name: 'Columbia \nUniversity', price: 1100.00, img: Columbia, tag: 'columbia', tag2: 'Columbia'},
{name: 'Wake Forest \nUniversity', price: 1100.00, img: Wake, tag: 'wake', tag2: 'wake forest'},
{name: 'Michigan, Ross', price: 1100.00, img: Ross, tag: 'ross', tag2: 'michigan'
}])
const [search, setSearch] = useState('')
const renderColleges = (colleges.sort()) => (colleges.map(colleges => <College {...colleges} />))
return (
<div>
<div className='title'>
<Input
className="input"
size="large"
placeholder="Search for a school"
onChange={(e) => setSearch(e.target.value)}
value={search}/>
</div>
<Row align="middle" className='container'>
{renderColleges(colleges.filter(c => c.name.includes(search) || c.tag.includes(search) || c.tag2.includes(search)))}
</Row>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.