anish
anish

Reputation: 7412

How to sort within array separated by Key with comma separated value

How to sort withins single array separated by Key as date with comma separated value.

The legacy Timeseries is sending data in the below format

arr1 = ['3/13/20,2179','3/14/20,2727', '3/14/20,1024' , '3/14/20,3020', '3/13/20,100', '3/13/20,190']

I'm struggling to sort it on below format the key is the date grouped by all the required values

arr1 = [3/13/20,2179,100,190,3/14/20,2727,1024,3020]

Any pointer/hint will be helpful.

Upvotes: 3

Views: 122

Answers (3)

xdeepakv
xdeepakv

Reputation: 8135

Iterate the date, split key to get date and count. Split date-string, if you want to sort on the date. and return diff of compare in sort callback function.

Sort based on date and then count

arr1 = [
  "3/13/20,2179",
  "3/14/20,2727",
  "3/14/20,1024",
  "3/14/20,3020",
  "3/13/20,100",
  "3/13/20,190",
];
const sort = (data) => {
  return data.sort((x, y) => {
    const [mm, dd, yy, num] = x.split(/[/,]/);
    const [mm1, dd1, yy1, num1] = y.split(/[/,]/);
    const d1 = new Date(dd, mm, yy).getTime();
    const d2 = new Date(dd1, mm1, yy1).getTime();
    if (d1 !== d1) return d1 - d2;
    return Number(num) - Number(num1);
  });
};
console.log(sort(arr1));

Updated: groupBy date

arr1 = [
  "3/13/20,2179",
  "3/14/20,2727",
  "3/14/20,1024",
  "3/14/20,3020",
  "3/13/20,100",
  "3/13/20,190",
];
const group = (data) => {
  const mapped = data.reduce((map, item) => {
    const [date, num] = item.split(",");
    if (!map[date]) map[date] = [];
    !map[date].push(num);
    return map;
  }, {});
  let result = [];
  for (const key in mapped) {
    const element = mapped[key];
    result = result.concat(key).concat(element);
  }
  return result;
};
console.log(group(arr1));

Upvotes: 3

Miguel Delgado
Miguel Delgado

Reputation: 11

const arr1 = [
  '3/13/20,2179',
  '3/14/20,2727',
  '3/14/20,1024',
  '3/14/20,3020',
  '3/13/20,100',
  '3/13/20,190',
];

const groupValuesByDate = (datesDictionary, dateWithValue) => {
  const [date, value] = dateWithValue.split(',');

  return {
    ...datesDictionary,
    [date]: datesDictionary[date] ? [...datesDictionary[date], parseInt(value, 10)] : [parseInt(value, 10)],
  };
};

const valuesGroupedByDate = arr1.reduce(groupValuesByDate, {});

let formattedDates = [];

for (date in valuesGroupedByDate) {
  const values = valuesGroupedByDate[date];
  formattedDates = [...formattedDates, date, ...values];
}

console.log(formattedDates);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386680

You could group by the first part and get a flat stringed result.

var data = ['3/13/20,2179', '3/14/20,2727', '3/14/20,1024', '3/14/20,3020', '3/13/20,100', '3/13/20,190'],
    result = Object
        .entries(data.reduce((r, s) => {
            const [date, value] = s.split(',');
            r[date] = r[date] || [];
            r[date].push(value);
            return r;
        }, {}))
        .flat(2);

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions