javascript - 使用对象在 Flatlist 中反应 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", textAlign: '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, alignItems: "center", textAlign: 'center', paddingBottom: '1%' }}>{item.loan} € pro Stunde</Text>
<Text style={{ fontSize: 18, alignItems: "center", textAlign: 'center', 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,
})
感谢所有可以提供帮助的人
解决方案
推荐阅读
- visual-studio-code - 错误:没有这样的文件(sftp liximomo 扩展名)
- r - EGARCH R 循环遍历数据框中的行
- c# - 如何在 ac# 应用程序中使用 PnP 框架
- html - 着陆页项目失败。很多问题和错误。有人可以解决并向我解释吗?
- python - NameError:名称'_'未在 django 中定义
- pdf - 任何用于 PDF 注释的 Ipad 应用程序将突出显示的文本复制到注释中(用于在 Docear 中导入)?
- java - Spring Cloud Data Flow:无法启动同一任务的多个实例
- excel - 如何阻止十进制数字在Excel中转换为日期
- c++ - 如何在 cmake (clion) 中从 mysql 添加额外的头文件?
- python - 在python中显示for循环迭代完成的百分比