How to define custom sort function in javascript?
It could be that the plugin is case-sensitive. Try inputting Te
instead of te
. You can probably have your results setup to not be case-sensitive. This question might help.
For a custom sort function on an Array
, you can use any JavaScript function and pass it as parameter to an Array
's sort()
method like this:
var array = ['White 023', 'White', 'White flower', 'Teatr'];
array.sort(function(x, y) {
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
});
// Teatr White White 023 White flower
document.write(array);
For Objects
try this:
function sortBy(field) {
return function(a, b) {
if (a[field] > b[field]) {
return -1;
} else if (a[field] < b[field]) {
return 1;
}
return 0;
};
}