3

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.

5
  • 2
    order ? of the sorting, on date ?? Commented Apr 12, 2020 at 7:32
  • 1
    Have you tried anything? It has to be within the array? Or you can allocate more? Something like this should take advantage of creating a map with the array where the dates are the keys and their value is an array of the values for each date. Commented Apr 12, 2020 at 7:33
  • 1
    please add a valid result. Commented Apr 12, 2020 at 8:14
  • @xdeepakv, check the Nina Answer Commented Apr 12, 2020 at 10:55
  • @anish I checked ur result and updated solution. if u still want can have a look. Commented Apr 12, 2020 at 12:43

3 Answers 3

3

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));

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the Answer, the second solution is very close to my Question
@anish i updated groupby solution, please check. this is optimized.
Thanks for the answer, This also help
1

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; }

1 Comment

This is very close to my Solution
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.