Custom sorting where some columns take priority
Custom sorting where some columns take priority
I need advice on implementing custom sorting. I am developing a message inbox module (similar to Outlook). Two of the message properties are boolean isPinned & isLocked. isPinned means the user wants the msg pinned to the top of grid. isLocked means it is pinned (isLocked = isPinned = true) by the organization which should display above user-pinned messages. The tricky part is that when a user clicks on the From/Sender/Date column headers for sorting, the sort algorithm needs to ALWAYS sort isLocked to the top, followed by isPinned, followed by whatever column the user clicked on.
I call the following Knockout function to initially sort the data (static data right now but eventually via Web API). I just need to figure out how to tweak column sorting to use something similar. Note: grid data is not editable, though the user can toggle pinned status (but not locked) and can click a trash icon to set isDeleted = true (it will be moved to the Trash folder).
self.sortMessageHeaders = function () {
self.messageHeaders.sort(function (left, right) {
// original sorting algorithm--compact version of the if..blocks below
//return left.isLocked() != right.isLocked() ? (left.isLocked() < right.isLocked() ? 1 : -1) : left.isPinned() != right.isPinned() ? (left.isPinned() < right.isPinned() ? 1 : -1) : left.dateSent() < right.dateSent() ? 1 : 0;
// sort by locked status if they are different
if (left.isLocked() != right.isLocked()) {
return (left.isLocked() < right.isLocked() ? 1 : -1);
}
// otherwise, sort by pinned status if they are different
if (left.isPinned() != right.isPinned()) {
return (left.isPinned() < right.isPinned() ? 1 : -1);
}
// as a last resort, sort by date/time sent
return (left.dateSent() < right.dateSent() ? 1 : -1);
});
};
Thanks for your time and attention.