What does the multiplication operator do in this code? - javascript

So I saw this code and have no clue what the * in sortBy[i].direction*(...) does. Can anyone break this down for me and help me understand this code?
result = sortBy[i].direction*(a[ sortBy[i].prop ] < b[ sortBy[i].prop ] ? -1 : (a[ sortBy[i].prop ] > b[ sortBy[i].prop ] ? 1 : 0));

The * operator multiples the value of sortBy[i].direction with
-1 if a[sortBy[i].prop] < b[sortBy[i].prop];
1 if a[sortBy[i].prop] > b[sortBy[i].prop];
0 otherwise.
Look at this as
result = sortBy[i].direction *
(a[sortBy[i].prop] < b[sortBy[i].prop]
? -1
: (a[sortBy[i].prop] > b[sortBy[i].prop]
? 1
: 0));

Related

Sort by Status then by date JavaScript

I have an array of the following type I wan to sort. I want to sort them by the status and show false ones first and and then sort them by their date. I don't know if I should use group by then sort.
"itemlist": [{
"Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test4"},{
"Status":false,"Date":"2021-07-23T07:28:23.841Z","Title":"test3"},{
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test2"},{
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test1"},{
"Status":false,"Date":"2021-07-23T06:46:34.614Z","Title":"test1"},{
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test0"},{
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test"}]
I have used the below code it sorts the array by status but the date sorting is not working. can someone help me with a better method and what am I doing wrong ? , Thanks
var res=itemlist.sort((a, b) => (a.Status === b.Status ) ? 0 : a.Status ? 1 : -1 || b.Date- a.Date);
The reason is that your date is string format,not actually date,you need to change it to date when compare
var res=itemlist.sort(
(a, b) => (a.Status === b.Status ) ?
0 : a.Status ? 1 : -1 || new Date(b.Date)- new Date(a.Date));
also,you expression is too complex to read and debug,had better to use () to wrap it or use if else condition instead
var res=itemlist.sort(
(a, b) => (a.Status === b.Status ) ?
0 : (a.Status ? 1 : -1 || new Date(b.Date)- new Date(a.Date)));
working code
var itemlist = [{
"Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test4"},{
"Status":false,"Date":"2021-07-23T07:28:23.841Z","Title":"test3"},{
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test2"},{
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test1"},{
"Status":false,"Date":"2021-07-23T06:46:34.614Z","Title":"test1"},{
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test0"},{
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test"}];
var res=itemlist.sort(
(a, b) => (a.Status === b.Status ) ?
0 : (a.Status ? 1 : -1 || new Date(b.Date)- new Date(a.Date)));
//output the sorted result
console.log(res);

Ternary Operator - 3 conditions

I'd like to rewrite this with a ternary operator. I believe I need 2 operators.
if (a.quantity > b.quantity) {
return -1;
} else if (a.quantity < b.quantity) {
return 1;
} else {
return 0;
}
Ternary
return (a.quantity > b.quantity) ? -1 : (a.quantity < b.quantity) ? 1 : 0;
would this be the equivalent?
If you need the value for sorting, you could take the delta of the two values:
data.sort((a, b) => a.quantity - b.quantity); // ascending
data.sort((a, b) => b.quantity - a.quantity); // descending
Yes those are equivalent
return (a.quantity > b.quantity) ? -1 : (a.quantity < b.quantity) ? 1 : 0;

ternary operation in js

Maybe I do not understand ternary operation but
if I am right it's
test ? true : false
So this should give
function toto(x, y)
{
return (x > 0 ? x < 7 ? true : false : false &&
y > 0 ? y < 6 ? true : false : false)
}
true only if 0
but if I do
toto(4,6)
it returns true, why? What am I missing ?
just do like this :
function toto(x, y)
{
return (x > 0 ? x < 7 ? true : false : false ) &&
( y > 0 ? y < 6 ? true : false : false)
}
with the bracket before and after the exp1 and exp2
and yes it's a bit unreadable ^^
edit : I also would do
return (x > 0 && x < 7) && (y > 0 && y < 6)
you need eslint to format your code ,this is the formatted code,see:
function toto(x, y) {
return x > 0
? x < 7
? true
: false
: false && y > 0
? y < 6
? true
: false
: false
}
image:
I think ,it is easier to understand
Aren't you trying to achieve this? chceking whether the x is from 0..7 and y is 0..6?
function toto(x, y)
{
return (x > 0 && x < 7) && (y > 0 && y < 6 );
}
Operator precedence affecting it here
function toto(x, y)
{
return ((x > 0 ? x < 7 ? true : false : false) && (y > 0 ? y < 6 ? true : false : false))
}

Can't create a haskell like macro in sweetjs

I want to create a macro like this
var diffLength =
| index1 === 0 then xPos[index1]
| index1 === 2 then (xPos[index1] - xPos[index1 - 1]) * focusFactor
| otherwise xPos[index1] - xPos[index1 - 1]
which should expand like :
var diffLength =
index1 === 0 ? xPos[index1] :
index1 === 2 ? (xPos[index1] - xPos[index1 - 1]) * focusFactor :
xPos[index1] - xPos[index1 - 1];
SweetJS code :
macro |{
rule {
$cond:expr then $check:expr $($guard:(|) $restCond:expr then $restCheck:expr) ... $lastGuard:(|) otherwise $lastCheck:expr
} => {
( $cond ? $check : $($restCond ? $restCheck :) ... $lastCheck )
}
}
which works only if I change $guards:(h) and $lastGuard:(h) insteed of $guards:(|) and $lastGuard:(|) and actual code like
var diffLength =
| index1 === 0 then xPos[index1]
h index1 === 2 then (xPos[index1] - xPos[index1 - 1]) * focusFactor
h otherwise xPos[index1] - xPos[index1 - 1]
Reason is I am unable to stop sweetjs to parse the adjacent "|"(pipe) delimiters.
Can anyone suggest how can I do it?

javascript broken syntax

Alright, I or someone I work with broke the syntax here somewhere, and I'm not sure where, as the debugger is giving me some random garble as the error. Anyway here is the function, I think I'm missing a bracket somewhere, but this is just evading me for some reason.
var sort_by = function(field, reverse, primer) {
var key = function (x) {return primer ? primer(x[field]) : x[field]};
return function (a,b) {
var A = key(a), B = key(b);
return ((A < B) ? -1 : (A > B) ? +1 : 0)) * [-1,1][+!!reverse];
}
}
there's an extra closing parenthesis on the line
return ((A < B) ? -1 : (A > B) ? +1 : 0))
should be
return ((A < B) ? -1 : (A > B) ? +1 : 0) ...etc
It would be useful if could provide the debugger error anyway. I exectued it in Chrome Developer Console and it gave the error:
SyntaxError: Unexpected token )
Which made it easy to find this broken line:
return ((A < B) ? -1 : (A > B) ? +1 : 0)) * [-1,1][+!!reverse];
You have unbalanced parenthesis. It should be:
return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse];
There's one extra closing bracket here. Remove it.
return ((A < B) ? -1 : (A > B) ? +1 : 0)) * [-1,1][+!!reverse];
Also, semicolon everything.
var sort_by = function(field, reverse, primer) {
var key = function(x) {
return primer ? primer(x[field]) : x[field];
};
return function(a, b) {
var A = key(a), B = key(b);
return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1, 1][+!!reverse];
};
};

Categories

Resources