【问题标题】:Sorting array by key works on iOS but not on Anroid按键排序数组适用于 iOS 但不适用于 Android
【发布时间】:2019-04-10 18:46:08
【问题描述】:

我想按日期升序对数组进行排序。它在 iOS 上完美运行,但在 Android 上无能为力。

    const sortedUnits = units.sort((b, a) => b.date > a.date);

【问题讨论】:

    标签: android ios arrays react-native


    【解决方案1】:

    尝试返回number而不是boolean的比较函数:

    const sortedUnits = units.sort((b, a) => b.date - a.date);
    

    (假设,date 字段为时间戳)

    【讨论】:

      【解决方案2】:
      units.sort(function (left, right) {
                return moment.utc(a.date).diff(moment.utc(b.date))
        });
      

      【讨论】:

        猜你喜欢
        • 2021-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多