I use Array.prototype.sort()
and Array.prototype.find()
:
var list = [];
list.push({'xid':'12345@kvm.dooth.com'});
list.push({'xid':'6789@kvm.dooth.com'});
list.push({'xid':'1357@kvm.dooth.com'});
list.push({'xid':'2468@kvm.dooth.com'});
var recent = [];
recent.push({'12345@kvm.dooth.com':3});
recent.push({'1357@kvm.dooth.com':1});
recent.push({'2468@kvm.dooth.com':2});
function sort(list, recent){
list.sort(function (a, b) {
a = getEmailValue(recent, a["xid"]);
b = getEmailValue(recent, b["xid"]);
return (a !== undefined) ? (a - b) : 1;
});
}
function getEmailValue(recent, email) {
var elem = recent.find(function (e) {
return e[email] !== undefined;
});
return elem && elem[email];
}
sort(list, recent);
console.log(list);
sort()
works by taking two elements from the array - a
and b
. It sorts them based on the return value of a compare function. If that function returns a positive number, a
is put after b
, if it's negative, a
is before b
, else they are unchanged.
In this:
return (a !== undefined) ? (a - b) : 1;
If a
is undefined
(getEmailValue()
has returned nothing), 1
is returned to sort it in the back. In your example, emails with no value in recent
are at the bottom of the list.
This:
return elem && elem[email];
Will return elem[email]
if elem
is not undefined
, else it will return undefined
. It's a way to prevent accessing the email
property of something that is not an object. For more info, check here.
Logical AND (&&) - expr1 && expr2 - Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.