Skip to content

Instantly share code, notes, and snippets.

@confraria
Created June 6, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save confraria/423f232f8431d02e1c8157f4ab395ba3 to your computer and use it in GitHub Desktop.
Save confraria/423f232f8431d02e1c8157f4ab395ba3 to your computer and use it in GitHub Desktop.
order
function orderBy(property) {
let prop = property;
if (_.isArray(prop)) {
prop = prop.join('.');
}
const [, sign, iterateeString] = prop.match(/^(-?)(.*)$/);
const order = sign === '-' ? 'desc' : 'asc';
let iteratee = iterateeString;
if (iteratee.match(/lastMeasurement_\d+/)) {
const key = iteratee;
iteratee = (asset) => {
let value = asset[key];
if (typeof value === 'string') {
value = parseInt(value, 10);
const orderMultiplier = order === 'asc' ? 1 : -1;
value = Number.isNaN(value) ? (orderMultiplier * Infinity) : value;
}else if (typeof value === 'object') {
value = value.value;
}
return value;
};
}
$scope.devices = _.orderBy($scope.devices, iteratee, order);
$scope.$broadcast('c8yScrollLoad::refresh');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment