// Reorder a file by running (from the scripts folder)

import fs from 'fs';
import {range} from 'lodash';
import {parse} from 'date-fns';
import {TOPICS} from '../src/components/config/index';

const args = process.argv;

const START_YEAR = 2017;
const CURRENT_YEAR = (new Date()).getYear() + 1900;
const conferencesJSON = {};

// Remove JavaScript key
const LANGUAGES = Object.keys(TOPICS);
LANGUAGES.splice(LANGUAGES.indexOf('javascript'), 1);
range(START_YEAR, CURRENT_YEAR + 1).forEach((year) => {
  conferencesJSON[year] = {};
  LANGUAGES.forEach((lang) => {
    conferencesJSON[year][lang] = {};
  });
});

Object.keys(conferencesJSON).forEach((year) => {
  Object.keys(conferencesJSON[year]).forEach((topic) => {
    const fileName = `./conferences/${year}/${topic}.json`;

    fs.readFile(fileName, (err, data) => {
      data = JSON.parse(data);
      data.sort((a,b) => {
        let startA = parse(a.startDate).getTime();
        let startB = parse(b.startDate).getTime();

        if (startA > startB) {
          return 1;
        }
        if (startA < startB) {
          return -1;
        }

        return 0;
      });

      fs.writeFile(fileName, JSON.stringify(data, null, 2), () => {
        console.log(`File ${fileName} was successfully reordered`);
      });
    });
  });
});

@@ -83,16 +83,24 @@
 t %7Brange
+, sortBy
 %7D from '
@@ -867,57 +867,66 @@
     
-data.sort((a,b) =%3E %7B%0A        let startA
+const newData = sortBy(data,%5B%0A        (conf)
  =
+%3E
  parse(
-a
+conf
 .sta
@@ -942,17 +942,17 @@
 etTime()
-;
+,
 %0A       
@@ -956,28 +956,44 @@
     
-let startB
+(conf)
  =
+%3E
  parse(
-b
+conf.endDate %7C%7C conf
 .sta
@@ -1013,160 +1013,32 @@
 me()
-;%0A%0A        if (startA %3E startB) %7B%0A          return 1;%0A        %7D%0A        if (startA %3C startB) %7B%0A          return -1;%0A        %7D%0A%0A        return 0;
+,%0A        'name'
 %0A      
-%7D
+%5D
 );%0A%0A
@@ -1081,17 +1081,20 @@
 ringify(
-d
+newD
 ata, nul
@@ -1192,20 +1192,23 @@
 ;%0A    %7D);%0A  %7D);%0A%7D);%0A
+%0A;%0A

Improve sorting algorithm by using sortBy from lodash