如何解决在带有对象的 Flatlist 中 React Native sort() 不起作用
我目前正在做一个学校项目,我需要按距离对数据进行排序,因为距离最近的人。我试图在数组上使用 sort() 函数,但它不起作用,我不知道为什么。
<FlatList
data={this.state.teachers.sort((a,b) => {return a.dis - b.dis})}
keyExtractor={item => item.ID}
renderItem={({ item }) => {
return <View style={styles.cardDesign}>
<Text style={{ fontWeight: 'bold',textTransform: 'capitalize',fontSize: 20,alignContent: "center",textAlign: 'center',paddingTop: '3%' }}>{item.fach}</Text>
<Text style={{ fontSize: 18,alignItems: "center",paddingBottom: '1%' }}>{item.firstname} {item.name}</Text>
<Image source={{ uri: item.picture}} style={{ flex: 100,resizeMode: 'contain',height: 200,width: "100%",paddingBottom: '5%' }} />
<Text style={{ fontSize: 18,paddingBottom: '1%' }}>{item.loan} € pro Stunde</Text>
<Text style={{ fontSize: 18,paddingBottom: '3%' }}>{item.distance} KM entfernt</Text>
<View style={styles.contactButtonContainerStyle}>
</View>
</View>;
}}
/>
这里是数据被推入数组的地方
this.setState({
teacher_lat: responseJson[i].latitude,})
this.setState({
teacher_lon: responseJson[i].longitude,})
var NotfachString = mathe + deutsch + englisch + politik + chemie + physik + "";
var fachString = NotfachString.substring(1);
var dis = getPreciseDistance(
{ latitude: this.state.teacher_lat,longitude: this.state.teacher_lon },{ latitude: this.state.latitude,longitude: this.state.longitude },);
dis = dis / 1000;
if (responseJson[i].ID != firebase.auth().currentUser.uid) {
if (subjectArray.length == 1) {
if (teacherFachID.includes(subjectArray[0].fachID) == true) {
if (this.state.entferung >= dis) {
var dis = dis.toFixed(1);
teachersArray.push({
id: responseJson[i].ID,firstname: responseJson[i].firstname,name: responseJson[i].name,email: responseJson[i].email,gender: responseJson[i].gender,fach: fachString,distance: dis,loan: responseJson[i].loan,picture: responseJson[i].picture,});
}
}
}
this.setState({
teachers: teachersArray,isVisible: false,})
感谢所有可以提供帮助的人
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。