简体   繁体   中英

Openui5: wrong sort order with sap.ui.model.Sorter

It appears sap.ui.model.Sorter doesn't sort ISO-8859-1 characters correctly.

Below is an example where we create a list with one item pr character in the Norwegian alfabet. The output of this is not in the correct order, instead the order is "AÅÆBCDEFGHIJKLMNOØPQRSTUVWXYZ".

The expected results is the same order as when the alfabet variable is declared: "ABCDEFGHIJKLMOPQRSTUVWXYZÆØÅ"

How can we sort the model correctly?

JSBIN: https://jsbin.com/xuyafu/

    var alfabet = "ABCDEFGHIJKLMOPQRSTUVWXYZÆØÅ"

var data = [];
for(var i=0; i< alfabet.length; i++){
  data.push ({value:alfabet.charAt(i)});
}

var modelList = new sap.ui.model.json.JSONModel(data);

sap.ui.getCore().setModel(modelList);

var oSorter = new sap.ui.model.Sorter("value", null, null);

       // Simple List in a Page
    new sap.m.App({
        pages: [
            new sap.m.Page({
                title: "Sorting with norwegian characters",
                content: [
                    new sap.m.List("list", {
                        items: {
                            path: '/',
                            template: new sap.m.StandardListItem({
                                title: '{value}'
                            }),
                            sorter: oSorter
                        }
                    })
                ]
            })
        ]
    }).placeAt("content");

上面代码的输出

Based on the input from the comments on the question, it is straight forward to override the sorting function fnCompare to get the right order

var oSorter = new sap.ui.model.Sorter("value", null, null);

oSorter.fnCompare   = function (a, b) {
        if (a == b) {
            return 0;
        }
        if (b == null) {
            return -1;
        }
        if (a == null) {
            return 1;
        }
        if (typeof a == "string" && typeof b == "string") {
            return a.localeCompare(b, "nb");
        }
        if (a < b) {
            return -1;
        }
        if (a > b) {
            return 1;
        }
        return 0;
    }

Here "nb" is the locale the sort is done with

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM