[英]Sort an array of object fetched from db rendered in a Flatlist React native
I have this array of object that i want to sort when i display them on my screen, sort them by type.here is a look of the array:我有这个 object 数组,我想在屏幕上显示它们时对其进行排序,按类型对它们进行排序。这里是数组的外观:
Array [
Object {
"lien": "get_praticien",
"name": "tag service centre 2",
"type": "Tag",
},
Object {
"lien": "get_praticien",
"name": "Ambulance",
"type": "Acte",
},
Object {
"lien": "get_praticien",
"name": "Réadaptation Fonctionnelle",
"type": "speciality",
},
Object {
"lien": "get_praticien",
"name": "Infirmité Motrice",
"type": "Tag",
},......
so i want the objects that have a type:"speciality" to be at the top of the flatlist, and the others after on whatever order.所以我希望具有 type:"speciality" 的对象位于平面列表的顶部,而其他对象则以任何顺序排列。 i'm using a flatlist:
我正在使用平面列表:
<FlatList
data={this.state.dataSource}
keyExtractor={item=> { return item.id.toString()}}
renderItem= {({item})=> <MedItem Med={item} />} />
i'll appreciate you answers我会很感激你的回答
use a sort function to sort your data list before rendering.在渲染之前使用排序 function 对数据列表进行排序。 Also you may want to sort them on server side if you have multiple pages of data.
如果您有多页数据,您也可能希望在服务器端对它们进行排序。
const test = [ { "lien": "get_praticien", "name": "tag service centre 2", "type": "Tag", }, { "lien": "get_praticien", "name": "Ambulance", "type": "Acte", }, { "lien": "get_praticien", "name": "Réadaptation Fonctionnelle", "type": "speciality", }, { "lien": "get_praticien", "name": "Infirmité Motrice", "type": "Tag", }, { "lien": "get_praticien", "name": "Infirmité Motrice", "type": "speciality", }, { "lien": "get_praticien", "name": "Ambulance", "type": "Acte", }, { "lien": "test", "name": "test Motrice", "type": "speciality", }, ] console.log(test.sort((a,b) => a.type === 'speciality'? -1: 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.