✅ – ekomS – 23-35 Dec 21

Howdy all. I've got this sort function which prioritizes truthy values in an array:
truthyFirst: (array, callback = emptyCallback) => {
return array.sort((a, b) => {
if (callback(a) && callback(b)) return 0;
if (callback(b)) return 1;
if (callback(a)) return -1;
});
},
truthyFirst: (array, callback = emptyCallback) => {
return array.sort((a, b) => {
if (callback(a) && callback(b)) return 0;
if (callback(b)) return 1;
if (callback(a)) return -1;
});
},
Is there a better way to coerce the condition to its relevant value? e.g. a && b -> 0, a -> -1, b -> 1
3 Replies
Owen Rossi-Keen
Owen Rossi-KeenOP2y ago
Got it Changed Lol, caught in the act Fair enough Thank you Another question, is there a better way to handle the emptyCallback bit? Currently I've got:
const emptyCallback = (...args) => args;
const emptyCallback = (...args) => args;
The idea being that if a callback is not provided to the function, it will return the parameters Are my arguments coerced into a boolean? Ah okay Well The value will always be a boolean
arrayFunctions.sort.truthyFirst(products, (product) => {
return product.metadata.status == 'In Stock';
})
arrayFunctions.sort.truthyFirst(products, (product) => {
return product.metadata.status == 'In Stock';
})
This is how I want to use the function Makes sense Perfect That's what I'm looking for Thank you I implemented both
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1055267274254209174
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1055267274254209174 Question not getting answered? Maybe it's hard to answer, or maybe you asked at a slow time. Check out these resources for help asking a good question: https://stackoverflow.com/help/how-to-ask http://wp.me/p2oIwo-26