Appcelerator Titanium / Alloy 型号:如何修改此排序方法以对包含数字的字符串进行排序

Appcelerator Titanium / Alloy Models: How do I modify this sorting method to sort a string that contains numbers

我在 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;
    }
};

这是我的 index.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');
        }