【问题标题】:Appcelerator Titanium / Alloy Models: How do I modify this sorting method to sort a string that contains numbersAppcelerator Titanium / Alloy Models:如何修改此排序方法以对包含数字的字符串进行排序
【发布时间】:2017-01-25 07:35:40
【问题描述】:

我在 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 等。

【问题讨论】:

    标签: javascript sorting titanium appcelerator titanium-alloy


    【解决方案1】:

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

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

    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');
            }
    

    【讨论】:

    • 我理解您的答案的概念,但我如何才能在我的代码的sortBy 函数中找出该字段是"date"?我不知道如何获取这些信息。
    • 我想我已经解释过了,所以这里是完整的功能:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2019-03-28
    相关资源
    最近更新 更多