codyc4321 codyc4321 - 1 month ago 22
Node.js Question

sort by converted item in lodash orderBy without changing it

I have data like

let data = [
{DATE: "2017-08-18", COUNT: "65", EVENT_NAME: "TAX_EXEMPTION_FAILURE"},
{DATE: "2017-08-18", COUNT: "63", EVENT_NAME: "FRAUD_NOT_DONE"},
{DATE: "2017-08-18", COUNT: "4451", EVENT_NAME: "FRAUD_ACCEPT"},
];


I want to sort by the number value of
COUNT
, like
65, 63, 4451
, as numbers and not strings. I can't change the data because it gets displayed in a table in the format it's in (I will sort
50%
data by
50
, but display
50%
, etc)

Based on Lodash : how to do a case insensitive sorting on a collection using orderBy?, I pass in
[item => parseInt(item)]
to
orderBy
.

the following script does not work:

const _ = require("lodash");
const orderBy = require('lodash/orderBy');
// import some from 'lodash/some';

function isNumber(item) {
let isnum = /^\d+$/.test(item);
return isnum;
}

function are_all_numbers(data, key) {
let values = _.map(data, key);
return _.every(values, isNumber);
}

let data = [
{DATE: "2017-08-18", COUNT: "65", EVENT_NAME: "TAX_EXEMPTION_FAILURE"},
{DATE: "2017-08-18", COUNT: "63", EVENT_NAME: "FRAUD_NOT_DONE"},
{DATE: "2017-08-18", COUNT: "4451", EVENT_NAME: "FRAUD_ACCEPT"},
];

let order = 'descending';

let all_are_numbers = are_all_numbers(data, 'COUNT');
console.log('all_are_numbers:');
console.log(all_are_numbers);

if (all_are_numbers) {
var orderByResults = orderBy(
data,
[item => parseInt(item)],
['desc']
);
} else {
var orderByResults = orderBy(
data,
prop,
order === 'descending' ? 'desc' : 'asc'
);
}

console.log(orderByResults);


It runs:

$ node test_script.js
all_are_numbers:
true
[ { DATE: '2017-08-18',
COUNT: '65',
EVENT_NAME: 'TAX_EXEMPTION_FAILURE' },
{ DATE: '2017-08-18', COUNT: '63', EVENT_NAME: 'FRAUD_NOT_DONE' },
{ DATE: '2017-08-18', COUNT: '4451', EVENT_NAME: 'FRAUD_ACCEPT' } ]


Why isn't it sorting by
parseInt
here?

Answer Source

You should tell orderBy to do the ordering by item.COUNT:

[item => parseInt(item.COUNT)],

orderBy = _.orderBy;

function isNumber(item) {
  let isnum = /^\d+$/.test(item);
  return isnum;
}

function are_all_numbers(data, key) {
  let values = _.map(data, key);
  return _.every(values, isNumber);
}

let data = [
  {DATE: "2017-08-18", COUNT: "65", EVENT_NAME: "TAX_EXEMPTION_FAILURE"},
  {DATE: "2017-08-18", COUNT: "63", EVENT_NAME: "FRAUD_NOT_DONE"},
  {DATE: "2017-08-18", COUNT: "4451", EVENT_NAME: "FRAUD_ACCEPT"},
];

let order = 'descending';

let all_are_numbers = are_all_numbers(data, 'COUNT');
console.log('all_are_numbers:');
console.log(all_are_numbers);

if (all_are_numbers) {
    var orderByResults = orderBy(
      data,
      [item => parseInt(item.COUNT)],
      ['desc']
    );
} else {
    var orderByResults = orderBy(
      data,
      prop,
      order === 'descending' ? 'desc' : 'asc'
    );
}

console.log(orderByResults);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>