Created
September 15, 2019 01:34
-
-
Save snoj/12b7ea0bafde699acb8ccd0f10eba248 to your computer and use it in GitHub Desktop.
multiple asc/desc sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function comp(a, b, n) { | |
let p1,p2,ret; | |
ret = 0; | |
ret += a < b; | |
ret -= a > b; | |
ret = convert(ret ? ret : 0, n); | |
return ret | |
p1 = ((a<b)/2); | |
p2 = convert(p1, n); | |
//console.log(a,b,n, 'c1', ((a<b)/2),'p1', p1, 'p2', p2); | |
return p2; | |
} | |
function convert(n, d) { | |
return n-((d*2)*n) | |
}; | |
function compcomp(a, b, sort) { | |
return _.reduce(sort, function(acc, n) { | |
let a1, b1; | |
a1 = _.get(a, n.n) | |
b1 = _.get(b, n.n) | |
let r = comp(a1,b1,n.d); | |
return (acc*2) + r; | |
}, 0); | |
} | |
t = [ | |
{n: 'joe', c: 1, t: 100}, | |
{n: 'susan', c: 54, t: 10}, | |
{n: 'alfred', c: 34, t: 7}, | |
{n: 'joe', c: 34, t: 4}, | |
{n: 'jack', c: 31, t: 2}, | |
{n: 'flo', c: 100, t: 4}, | |
{n: 'moe', c: 2, t: 1}, | |
] | |
sorts = [ | |
{n: 't', d: true}, | |
{n: 'c', d: true}, | |
] | |
t.sort(function(a,b) { | |
let t = compcomp(a,b,sorts); | |
console.log(t); | |
return t; | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment