Last active
January 28, 2017 12:36
-
-
Save cschuff/346b65059138278b4aa7c1677f73776d to your computer and use it in GitHub Desktop.
A custom comparator that converts strings to number and compares them. Demonstrates usage in sap.ui.model.Sorter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// String to Number Comparator | |
var oSorter = new sap.ui.model.Sorter("path"); | |
oSorter.fnCompare = function (a, b) { | |
var valA = parseInt(a, 10); | |
var valB = parseInt(b, 10); | |
if (isNaN(valA)) return 1; | |
if (isNaN(valB)) return -1; | |
if (isNaN(valA) && isNaN(valB)) return 0; | |
return valA - valB; | |
} | |
// newer SAPUI5 versions > 1.36 | |
// new sap.ui.model.Sorter(sPath, bDescending?, vGroup?, fnComparator?) | |
var oSorter = new sap.ui.model.Sorter("path", true, false, function (a, b) { | |
var valA = parseInt(a, 10); | |
var valB = parseInt(b, 10); | |
if (isNaN(valA)) return 1; | |
if (isNaN(valB)) return -1; | |
if (isNaN(valA) && isNaN(valB)) return 0; | |
return valA - valB; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment