I have a json like:
[
{
"id": 815785925,
"title": "fish /home/ismail",
"wm_class": "Alacritty",
"icon": "Alacritty",
"workspace_id": 1,
"workspace_name": "Codium/Books"
},
{
"id": 815785924,
"title": "journalctl /home/ismail/.dotfiles/.debian-dotfiles/.local/share/gnome-shell/extensions/gnome-utils-by-blueray453",
"wm_class": "Alacritty",
"icon": "Alacritty",
"workspace_id": 1,
"workspace_name": "Codium/Books"
},
{
"id": 815785920,
"title": "windowFunctions.js - learning-reading-memory-mastery-deliberate-practice - VSCodium",
"wm_class": "VSCodium",
"icon": "vscodium",
"workspace_id": 1,
"workspace_name": "Codium/Books"
},
{
"id": 815785919,
"title": "Get Workspace Name — Mozilla Firefox",
"wm_class": "firefox",
"icon": "/opt/firefox/browser/chrome/icons/default/default128.png",
"workspace_id": 1,
"workspace_name": "Codium/Books"
},
{
"id": 815785918,
"title": "FSearch",
"wm_class": "Fsearch",
"icon": "io.github.cboxdoerfer.FSearch",
"workspace_id": 1,
"workspace_name": "Codium/Books"
}
]
I want to sort it based on wm_class where Fsearch will come first, then VSCodium then firefox. Rest of the windows afterwards. No need to sort those (Rest of the windows). how would the compare function look like, so that I can run myArray.sort(compareFunction)
?
I tried:
myArray.sort((winA, winB) => {
// Define the order of wm_classes
const classOrder = {
"Fsearch": 0,
"VSCodium": 1,
"firefox": 2
};
let orderA = classOrder[winA.wm_class] || Infinity;
let orderB = classOrder[winB.wm_class] || Infinity;
return orderA - orderB;
});
but not giving the expected result.