【问题标题】:React Native sort() in Flatlist with Objects dont work使用对象在 Flatlist 中反应 Native sort() 不起作用
【发布时间】:2021-05-16 01:55:33
【问题描述】:

我目前正在做一个学校项目,我需要按距离对数据进行排序,因为距离最近的人。我尝试在数组上使用 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,
                })


感谢所有可以提供帮助的人

【问题讨论】:

    标签: javascript react-native sorting object


    【解决方案1】:

    排序函数需要返回以下之一:-101

    我相信您的排序函数返回的值可能更高或更低。

    Reference

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2021-05-24
      • 2021-09-04
      相关资源
      最近更新 更多