Skip to content

Instantly share code, notes, and snippets.

@dvvtms
Last active June 21, 2022 10:27
Show Gist options
  • Save dvvtms/b4ea7b06aa864eb166a6fadf0b6fe74c to your computer and use it in GitHub Desktop.
Save dvvtms/b4ea7b06aa864eb166a6fadf0b6fe74c to your computer and use it in GitHub Desktop.
coderama
function sumNumbersInNestedArray(inp: any) {
  let cache = 0;

  const sumRecursive = (args: any) => {
    if (Array.isArray(args)) {
      for (let i = 0; i < args.length; i++) {
        sumRecursive(args[i]);
      }
    } else if (!isNaN(args)) {
      cache = cache + args;
    }
  };

  sumRecursive(inp);

  return cache;
}


import { addDays, isBefore } from 'date-fns';
const items = [
  {
    id: '2',
    name: '2-name',
    amount: 5,
    eta: addDays(new Date(), 1),
    status: 'active',
  },
  {
    id: '3',
    name: '3-name',
    amount: 5,
    eta: addDays(new Date(), 2),
    status: 'upcoming',
  },
  {
    id: '6',
    name: '3-name',
    amount: 5,
    eta: addDays(new Date(), 2),
    status: 'upcoming',
  },
  {
    id: '4',
    name: '3-name',
    amount: 5,
    eta: addDays(new Date(), 3),
    status: 'pending',
  },
  {
    id: '1',
    name: '1-name',
    amount: 5,
    eta: new Date(),
    status: 'active',
  },
  {
    id: '5',
    name: '5-name',
    amount: 5,
    eta: new Date(),
    status: 'pending',
  },
];

function itemArranger(args: any) {
  return [...args].sort((a, b) => {
    if (a.status === 'active') {
      if (b.status === 'active') {
        return isBefore(a.eta, b.eta) ? -1 : 0;
      }
      return -1;
    } else if (a.status === 'upcoming') {
      if (b.status === 'upcoming') {
        return isBefore(a.eta, b.eta) ? -1 : 0;
      } else if (b.status === 'active') {
        return 1;
      }
      return -1;
    } else if (a.status === 'pending') {
      if (b.status === 'pending') {
        return isBefore(a.eta, b.eta) ? -1 : 0;
      } else {
        return 1;
      }
    }
  });
}

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment