Appcelerator钛/合金模型:如何修改此排序方法以对包含数字的字符串进行排序



我在Appcelerator中创建了一个模型,该模型具有排序方法。其中一个属性(date)是一个只包含数字的字符串。我需要按升序或降序对此进行排序,就好像它是一个整数一样。但是,由于该字段是 UTC 日期时间戳,因此有时较旧的日期具有较少的数字并且以较大的数字开头,因此在按最新排序时,这些日期位于顶部,或在按最旧排序时位于底部。

这是我的模型文件:

exports.definition = {
    config: {
        columns: {
            "name": "text",
            "rating": "integer",
            "location": "text",
            "notes": "text",
            "date": "text",
            "latitude": "integer",
            "longitude": "integer",
            "api_id": "text"  
        },
        adapter: {
            type: "sql",
            collection_name: "test"
        }
    },
    extendModel: function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
        });
        return Model;
    },
    extendCollection: function(Collection) {
        _.extend(Collection.prototype, {
            // extended functions and properties go here
            initialize: function () {
                 this.sortField = "date";
                 this.sortDirection = "DESC";
            },
            comparator: function (collection) {
                 var that = this;
                 return collection.get(that.sortField);
            },
            setSortField: function (field, direction) {
                this.sortField = field;
                this.sortDirection = direction;
            },
            sortBy: function (iterator, context) {
                var obj = this.models;
                var direction = this.sortDirection;
                return _.pluck(_.map(obj, function (value, index, list) {
                    return {
                        value: value,
                        index: index,
                        criteria: iterator.call(context, value, index, list)
                    };
                }).sort(function (left, right) {
                    // swap a and b for reverse sort
                    var a = direction === "ASC" ? left.criteria : right.criteria;
                    var b = direction === "ASC" ? right.criteria : left.criteria;
                    if (a !== b) {
                        if (a > b || a === void 0) return 1;
                        if (a < b || b === void 0) return -1;
                    }
                    return left.index < right.index ? -1 : 1;
                }), 'value');
            }
        });
        return Collection;
    }
};

这是我索引中的代码.js用于对啤酒进行排序

theBeers.setSortField("date", "ASC");
theBeers.sort();

如何修改上面的代码来解决此问题?

请记住,我仍然需要其他字符串属性进行正常排序,例如namelocation等。

最好的方法是在比较之前将日期转换为整数,简单地说:

您需要为排序字段声明一个局部变量,与方向相同:

var field = this.sortField;

在 if 语句之前,添加以下内容:

if (field == "date") {
    a = parseInt(a);
    b = parseInt(b);
}

可能需要像在代码中一样添加对 void 的检查。

所以完整的答案:

    sortBy: function (iterator, context) {
            var obj = this.models;
            var direction = this.sortDirection;
            var field = this.sortField;
            return _.pluck(_.map(obj, function (value, index, list) {
                return {
                    value: value,
                    index: index,
                    criteria: iterator.call(context, value, index, list)
                };
            }).sort(function (left, right) {
                // swap a and b for reverse sort
                var a = direction === "ASC" ? left.criteria : right.criteria;
                var b = direction === "ASC" ? right.criteria : left.criteria;
                if (field == "date") {
                    a = parseInt(a);
                    b = parseInt(b);
                }
                if (a !== b) {
                    if (a > b || a === void 0) return 1;
                    if (a < b || b === void 0) return -1;
                }
                return left.index < right.index ? -1 : 1;
            }), 'value');
        }

最新更新