Sort array by month and yearCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How do I sort a dictionary by value?Sorting an array of JavaScript objects by propertySort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?

Facing a paradox: Earnshaw's theorem in one dimension

Could gravitational lensing be used to protect a spaceship from a laser?

How can I tell someone that I want to be his or her friend?

What to put in ESTA if staying in US for a few days before going on to Canada

Why do bosons tend to occupy the same state?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Alternative to sending password over mail?

Today is the Center

What's the difference between 'rename' and 'mv'?

Is the Joker left-handed?

Assassin's bullet with mercury

Would Slavery Reparations be considered Bills of Attainder and hence Illegal?

In Romance of the Three Kingdoms why do people still use bamboo sticks when paper had already been invented?

Is "remove commented out code" correct English?

Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?

Were any external disk drives stacked vertically?

Has there ever been an airliner design involving reducing generator load by installing solar panels?

How could indestructible materials be used in power generation?

What is going on with Captain Marvel's blood colour?

Does a druid starting with a bow start with no arrows?

What exploit are these user agents trying to use?

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?



Sort array by month and year


Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How do I sort a dictionary by value?Sorting an array of JavaScript objects by propertySort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








18















I need help with sorting an array by month and year to display on chart respectively.



Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....];
Desired Output : [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];


I also have another array with values for each month according to the array above respectively



Array2: ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
,'Jan19_value'....];

Array2: ['55','2','3','0','21','132'....]; //real values


When the monthyear array sorts, I want the data in this array to move to a new position according to the monthyear position. Like this:



Desired Array1: [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
Desired Array2: [....'Apr18_value','May18_value','Jun18_value','Jul18_value','Jan19_value','Mar19_value'];


So I can pick the data later like this:



var label = array[4];
var value = array2[4];


How can this be accomplished?










share|improve this question



















  • 11





    What have you tried so far?

    – Anurag Srivastava
    Mar 8 at 10:36

















18















I need help with sorting an array by month and year to display on chart respectively.



Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....];
Desired Output : [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];


I also have another array with values for each month according to the array above respectively



Array2: ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
,'Jan19_value'....];

Array2: ['55','2','3','0','21','132'....]; //real values


When the monthyear array sorts, I want the data in this array to move to a new position according to the monthyear position. Like this:



Desired Array1: [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
Desired Array2: [....'Apr18_value','May18_value','Jun18_value','Jul18_value','Jan19_value','Mar19_value'];


So I can pick the data later like this:



var label = array[4];
var value = array2[4];


How can this be accomplished?










share|improve this question



















  • 11





    What have you tried so far?

    – Anurag Srivastava
    Mar 8 at 10:36













18












18








18


3






I need help with sorting an array by month and year to display on chart respectively.



Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....];
Desired Output : [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];


I also have another array with values for each month according to the array above respectively



Array2: ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
,'Jan19_value'....];

Array2: ['55','2','3','0','21','132'....]; //real values


When the monthyear array sorts, I want the data in this array to move to a new position according to the monthyear position. Like this:



Desired Array1: [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
Desired Array2: [....'Apr18_value','May18_value','Jun18_value','Jul18_value','Jan19_value','Mar19_value'];


So I can pick the data later like this:



var label = array[4];
var value = array2[4];


How can this be accomplished?










share|improve this question
















I need help with sorting an array by month and year to display on chart respectively.



Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....];
Desired Output : [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];


I also have another array with values for each month according to the array above respectively



Array2: ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
,'Jan19_value'....];

Array2: ['55','2','3','0','21','132'....]; //real values


When the monthyear array sorts, I want the data in this array to move to a new position according to the monthyear position. Like this:



Desired Array1: [....'Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
Desired Array2: [....'Apr18_value','May18_value','Jun18_value','Jul18_value','Jan19_value','Mar19_value'];


So I can pick the data later like this:



var label = array[4];
var value = array2[4];


How can this be accomplished?







javascript html arrays sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 15:48









Pikachu the Purple Wizard

2,03161429




2,03161429










asked Mar 8 at 10:32









Amir QureshiAmir Qureshi

457114




457114







  • 11





    What have you tried so far?

    – Anurag Srivastava
    Mar 8 at 10:36












  • 11





    What have you tried so far?

    – Anurag Srivastava
    Mar 8 at 10:36







11




11





What have you tried so far?

– Anurag Srivastava
Mar 8 at 10:36





What have you tried so far?

– Anurag Srivastava
Mar 8 at 10:36












9 Answers
9






active

oldest

votes


















10














You could get the date as a sortable string and sort it.



For getting more than one array sorted by one signature array, you could take sorting with map, where you sort an array of indices, indicating the final sorting and then reassign all arrays with this sorting.



The getD function returns a formatted string by taking an index of array0 for sorting. Inside of the function the string is destructed into month and year parts, and replaced by its ISO 8601 representation. The replacement function callback takes the matched items, returns an array with a formatted year and the month of an object with the month names and the related month numbers. Then, this array is joined and returned.



Sorting takes place with a comparison with String#localeCompare.






var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
array3 = ['55', '2', '3', '0', '21', '132'],
indices = Object
.keys(array1)
.sort(function (a, b)
function getD(i)
var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
s = array1[i];
return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

return getD(a).localeCompare(getD(b));
),
result = [array1, array3].map(a => indices.map(i => a[i]));

result.forEach(a => console.log(...a));








share|improve this answer




















  • 1





    Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

    – prasanth
    Mar 8 at 11:36







  • 1





    @prasanth, thank you for the hint. i thought, the number is a day.

    – Nina Scholz
    Mar 8 at 11:40






  • 1





    if i only want to perform this with array 1 and array 3?

    – Amir Qureshi
    Mar 8 at 12:05







  • 1





    @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

    – Nina Scholz
    Mar 8 at 12:08







  • 1





    do you want to get new arrays?

    – Nina Scholz
    Mar 8 at 12:15


















13














You need change the string to new Date(dateString) format like



new Date(Month Date Year)


Updated regex Pattern for Both Array



https://regex101.com/r/h1cm1z/2/



Updated Sort Second array based on first array sorting index






var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
var arr2 =['55','2','3','0','21','132'];

function datesort(arr)
return arr.concat().sort((a,b)=>
a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
return new Date(a) - new Date(b)
)


var after_arr1 =new datesort(arr1);
var after_arr2 = arr1.reduce(function(a,b,c)
var ind = after_arr1.indexOf(b);
a[ind] = arr2[c]
return a
,[]);


console.log(after_arr1.join(','));
console.log(after_arr2.join(','))








share|improve this answer




















  • 1





    Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

    – Amir Qureshi
    Mar 8 at 10:48






  • 1





    i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

    – prasanth
    Mar 8 at 10:53






  • 1





    Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

    – prasanth
    Mar 8 at 10:56







  • 1





    i have updated the question please have a look .

    – Amir Qureshi
    Mar 8 at 11:01






  • 2





    you need to sort array2 based on array1 sorted index position right?

    – prasanth
    Mar 8 at 11:17


















10














To order properly you need to know the order of the months. This is not alphabetical so you can use an array with the order of the months then look them up.



const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']



Is already properly sorted and can be used in conjunction with .indexOf() to get the position of a month.






const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
,'Jan19_value'];

const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

let sortYearMonth = (a, b) =>
let monthA = monthOrder.indexOf(a.slice(0,3))
let yearA = a.slice(3,6)
let monthB = monthOrder.indexOf(b.slice(0,3))
let yearB = b.slice(3,6)
return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


let sortedMonths = myArr.sort(sortYearMonth)
let sortedMonths2 = myArr2.sort(sortYearMonth)

console.log(sortedMonths )
console.log(sortedMonths2 )





Update: values in same position



Updated version links the two arrays together then sorts the first while keeping the relative position to the second.



Idea: Link two arrays with a temporary Object then extract the key/value pair using Object.entries. Then sorting the array based on the first value of the pair, which is the value of array1. Then it returns the key/value pair in the right order you can extract the values into two arrays again by using .map()



I added a run with the string based example and the real values examples below




const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

const myArr3 = ['55','2','3','0','21','132'];

const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

let sortYearMonth = (a, b) =>
let monthA = monthOrder.indexOf(a.slice(0, 3))
let yearA = a.slice(3, 6)
let monthB = monthOrder.indexOf(b.slice(0, 3))
let yearB = b.slice(3, 6)
return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


function sortByFirst(myArr, myArr2)
let keyValue = myArr.reduce((links, item, i) =>
links[item] = myArr2[i];
return links
, )
let entries = Object.entries(keyValue)
return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


let sortedEntries = sortByFirst(myArr, myArr2)
let sortedMonths = sortedEntries.map(i => i[0])
let sortedValues = sortedEntries.map(i => i[1])

let sortedEntries2 = sortByFirst(myArr, myArr3)
let sortedMonths2 = sortedEntries2.map(i => i[0])
let sortedValues2 = sortedEntries2.map(i => i[1])

console.log(sortedMonths)
console.log(sortedValues)

console.log(sortedMonths2)
console.log(sortedValues2)








share|improve this answer
































    10

















    const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

    //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
    //Can be done easily by using momentjs, darte-fns, but here i will do it natively

    const t =
    Jan: 1,
    Feb: 2,
    Mar: 3,
    Apr: 4,
    May: 5,
    Jun: 6,
    Jul: 7,
    Aug: 8,
    Sep: 9,
    Oct: 10,
    Nov: 11,
    Dec: 12

    const giveConcatString = (a, t) =>
    const monthPart = a.substr(0, 3)
    const yearPart = a.substr(3)
    return `$yearPart$t[monthPart]`

    const sortedArray = input.sort((a, b) =>
    const concatString = giveConcatString(a, t)
    const concatStringB = giveConcatString(b, t)
    return concatString <= concatStringB ? -1 : 1

    )
    console.log(sortedArray)





    This may help you solve this problem. Did it natively.






    share|improve this answer
































      4














      You can try this raw snippet.






      var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
      var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
      // converting to raw date format
      for (i in MY)
      num = MY[i].match(/d+/g);
      letr = MY[i].match(/[a-zA-Z]+/g);
      MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

      // sorting logic
      var sorted = MY.sort(function(a, b)
      a = a.split("-");
      b = b.split("-")
      return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
      );
      // converting back to original array after sorting
      res = [];
      for (i in sorted)
      var a = sorted[i].split('-');
      res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


      console.log(res);








      share|improve this answer






























        3














        I used lodash for sorting and substring method to divide date into month and day parts






        const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

        const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

        const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

        console.log(sorted)

        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








        share|improve this answer






























          3














          let mth = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];

          mth.sort((itemA, itemB)=>
          let mthAstr = itemA.slice(0,3)+" 01 "+itemA.slice(3,5);
          let mthA = new Date(mthAstr);
          let mthBstr = itemB.slice(0,3)+" 01 "+itemB.slice(3,5);
          let mthB = new Date(mthBstr);
          if (mthA < mthB)
          return -1;
          if (mthA > mthB)
          return 1;
          return 0;
          );





          share|improve this answer






























            3














            You can try something like that :



            var Array = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
            Array.sort(function(a, b)
            a = [a.slice(0,3), ' 20', a.slice(3)].join('');
            b = [b.slice(0,3), ' 20', b.slice(3)].join('')
            return new Date() - new Date(b);
            );

            console.log(Array);





            share|improve this answer
































              1














              I would suggest that your data structure is unfortunate. Working with shared indices makes it much more difficult than a data structure that combines them, such as an array of objects, [label: 'Mar19', value: 55, ...].



              If your data is coming from an upstream solution you cannot control, you can still manage this in your own work, converting before you use it. (And if you really have to converting back to pass to others.)



              A common name for a function combining two arrays is zip -- think of it acting like a zipper. This version uses one that takes a function to say how the paired elements should be combined. (Elsewhere such a function might be called zipWith.)



              Here sortArraysByDate calls zip passing a function that turns 'Mar19' and 55 into label: 'Mar19, value: 55, month: 'Mar', year: 19 using dateFields to extract the month and year from that label, then sorts these using the straightforward dateSort






              const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
              Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

              const dateFields = (d) => (
              month: d.slice(0, 3),
              year: Number(d.slice(3))
              )

              const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
              (y1 - y2) || (months[m1] - months[m2])

              const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

              const sortArraysByDate = (a1, a2) =>
              zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
              .sort(dateSort)
              .map((label, value) => (label, value))


              const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
              const Array2 = ['55','2','3','0','21','132']; //real values

              const result = sortArraysByDate(Array1, Array2)

              console.log(result)





              The map call in sortArraysByDate is quite possibly not necessary. Without it, the resulting data includes extra year and month fields; that might not be an issue.



              If you really need those updated arrays in the original format, you can just map the result:



              const newArray1 = result.map(o => o.label)
              const newArray2 = result.map(o => o.value)


              But I would urge you not to do this unless it's absolutely necessary. This structure is really useful. The paired arrays are much less so.



              Also, if you need to combine more than two arrays, you could write a slightly more sophisticated version of zip:



              const zip = (fn, ...as) => as[0].map((_, i) => fn(...as.map(a => a[i])))


              This takes a function on n arguments, and n arrays, and yields a new array containing the result of calling that function respectively on the successive items in each of the arrays.






              share|improve this answer

























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061324%2fsort-array-by-month-and-year%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                9 Answers
                9






                active

                oldest

                votes








                9 Answers
                9






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                10














                You could get the date as a sortable string and sort it.



                For getting more than one array sorted by one signature array, you could take sorting with map, where you sort an array of indices, indicating the final sorting and then reassign all arrays with this sorting.



                The getD function returns a formatted string by taking an index of array0 for sorting. Inside of the function the string is destructed into month and year parts, and replaced by its ISO 8601 representation. The replacement function callback takes the matched items, returns an array with a formatted year and the month of an object with the month names and the related month numbers. Then, this array is joined and returned.



                Sorting takes place with a comparison with String#localeCompare.






                var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
                array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
                array3 = ['55', '2', '3', '0', '21', '132'],
                indices = Object
                .keys(array1)
                .sort(function (a, b)
                function getD(i)
                var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
                s = array1[i];
                return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

                return getD(a).localeCompare(getD(b));
                ),
                result = [array1, array3].map(a => indices.map(i => a[i]));

                result.forEach(a => console.log(...a));








                share|improve this answer




















                • 1





                  Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

                  – prasanth
                  Mar 8 at 11:36







                • 1





                  @prasanth, thank you for the hint. i thought, the number is a day.

                  – Nina Scholz
                  Mar 8 at 11:40






                • 1





                  if i only want to perform this with array 1 and array 3?

                  – Amir Qureshi
                  Mar 8 at 12:05







                • 1





                  @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

                  – Nina Scholz
                  Mar 8 at 12:08







                • 1





                  do you want to get new arrays?

                  – Nina Scholz
                  Mar 8 at 12:15















                10














                You could get the date as a sortable string and sort it.



                For getting more than one array sorted by one signature array, you could take sorting with map, where you sort an array of indices, indicating the final sorting and then reassign all arrays with this sorting.



                The getD function returns a formatted string by taking an index of array0 for sorting. Inside of the function the string is destructed into month and year parts, and replaced by its ISO 8601 representation. The replacement function callback takes the matched items, returns an array with a formatted year and the month of an object with the month names and the related month numbers. Then, this array is joined and returned.



                Sorting takes place with a comparison with String#localeCompare.






                var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
                array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
                array3 = ['55', '2', '3', '0', '21', '132'],
                indices = Object
                .keys(array1)
                .sort(function (a, b)
                function getD(i)
                var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
                s = array1[i];
                return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

                return getD(a).localeCompare(getD(b));
                ),
                result = [array1, array3].map(a => indices.map(i => a[i]));

                result.forEach(a => console.log(...a));








                share|improve this answer




















                • 1





                  Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

                  – prasanth
                  Mar 8 at 11:36







                • 1





                  @prasanth, thank you for the hint. i thought, the number is a day.

                  – Nina Scholz
                  Mar 8 at 11:40






                • 1





                  if i only want to perform this with array 1 and array 3?

                  – Amir Qureshi
                  Mar 8 at 12:05







                • 1





                  @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

                  – Nina Scholz
                  Mar 8 at 12:08







                • 1





                  do you want to get new arrays?

                  – Nina Scholz
                  Mar 8 at 12:15













                10












                10








                10







                You could get the date as a sortable string and sort it.



                For getting more than one array sorted by one signature array, you could take sorting with map, where you sort an array of indices, indicating the final sorting and then reassign all arrays with this sorting.



                The getD function returns a formatted string by taking an index of array0 for sorting. Inside of the function the string is destructed into month and year parts, and replaced by its ISO 8601 representation. The replacement function callback takes the matched items, returns an array with a formatted year and the month of an object with the month names and the related month numbers. Then, this array is joined and returned.



                Sorting takes place with a comparison with String#localeCompare.






                var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
                array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
                array3 = ['55', '2', '3', '0', '21', '132'],
                indices = Object
                .keys(array1)
                .sort(function (a, b)
                function getD(i)
                var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
                s = array1[i];
                return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

                return getD(a).localeCompare(getD(b));
                ),
                result = [array1, array3].map(a => indices.map(i => a[i]));

                result.forEach(a => console.log(...a));








                share|improve this answer















                You could get the date as a sortable string and sort it.



                For getting more than one array sorted by one signature array, you could take sorting with map, where you sort an array of indices, indicating the final sorting and then reassign all arrays with this sorting.



                The getD function returns a formatted string by taking an index of array0 for sorting. Inside of the function the string is destructed into month and year parts, and replaced by its ISO 8601 representation. The replacement function callback takes the matched items, returns an array with a formatted year and the month of an object with the month names and the related month numbers. Then, this array is joined and returned.



                Sorting takes place with a comparison with String#localeCompare.






                var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
                array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
                array3 = ['55', '2', '3', '0', '21', '132'],
                indices = Object
                .keys(array1)
                .sort(function (a, b)
                function getD(i)
                var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
                s = array1[i];
                return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

                return getD(a).localeCompare(getD(b));
                ),
                result = [array1, array3].map(a => indices.map(i => a[i]));

                result.forEach(a => console.log(...a));








                var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
                array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
                array3 = ['55', '2', '3', '0', '21', '132'],
                indices = Object
                .keys(array1)
                .sort(function (a, b)
                function getD(i)
                var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
                s = array1[i];
                return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

                return getD(a).localeCompare(getD(b));
                ),
                result = [array1, array3].map(a => indices.map(i => a[i]));

                result.forEach(a => console.log(...a));





                var array1 = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'],
                array2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'],
                array3 = ['55', '2', '3', '0', '21', '132'],
                indices = Object
                .keys(array1)
                .sort(function (a, b)
                function getD(i)
                var months = Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' ,
                s = array1[i];
                return s.replace(/^(...)(.+)$/, (_, m, y) => [y.padStart(4, '0'), months[m]].join('-'));

                return getD(a).localeCompare(getD(b));
                ),
                result = [array1, array3].map(a => indices.map(i => a[i]));

                result.forEach(a => console.log(...a));






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 11 at 6:12









                vahdet

                2,18451636




                2,18451636










                answered Mar 8 at 10:50









                Nina ScholzNina Scholz

                196k15108179




                196k15108179







                • 1





                  Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

                  – prasanth
                  Mar 8 at 11:36







                • 1





                  @prasanth, thank you for the hint. i thought, the number is a day.

                  – Nina Scholz
                  Mar 8 at 11:40






                • 1





                  if i only want to perform this with array 1 and array 3?

                  – Amir Qureshi
                  Mar 8 at 12:05







                • 1





                  @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

                  – Nina Scholz
                  Mar 8 at 12:08







                • 1





                  do you want to get new arrays?

                  – Nina Scholz
                  Mar 8 at 12:15












                • 1





                  Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

                  – prasanth
                  Mar 8 at 11:36







                • 1





                  @prasanth, thank you for the hint. i thought, the number is a day.

                  – Nina Scholz
                  Mar 8 at 11:40






                • 1





                  if i only want to perform this with array 1 and array 3?

                  – Amir Qureshi
                  Mar 8 at 12:05







                • 1





                  @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

                  – Nina Scholz
                  Mar 8 at 12:08







                • 1





                  do you want to get new arrays?

                  – Nina Scholz
                  Mar 8 at 12:15







                1




                1





                Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

                – prasanth
                Mar 8 at 11:36






                Are you sure the sorting is correct format? i think you are sorted MMDD Format its a MMYY

                – prasanth
                Mar 8 at 11:36





                1




                1





                @prasanth, thank you for the hint. i thought, the number is a day.

                – Nina Scholz
                Mar 8 at 11:40





                @prasanth, thank you for the hint. i thought, the number is a day.

                – Nina Scholz
                Mar 8 at 11:40




                1




                1





                if i only want to perform this with array 1 and array 3?

                – Amir Qureshi
                Mar 8 at 12:05






                if i only want to perform this with array 1 and array 3?

                – Amir Qureshi
                Mar 8 at 12:05





                1




                1





                @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

                – Nina Scholz
                Mar 8 at 12:08






                @AmirQureshi, then you need oly the wanted array in the array for getting a new sort. please see edit.

                – Nina Scholz
                Mar 8 at 12:08





                1




                1





                do you want to get new arrays?

                – Nina Scholz
                Mar 8 at 12:15





                do you want to get new arrays?

                – Nina Scholz
                Mar 8 at 12:15













                13














                You need change the string to new Date(dateString) format like



                new Date(Month Date Year)


                Updated regex Pattern for Both Array



                https://regex101.com/r/h1cm1z/2/



                Updated Sort Second array based on first array sorting index






                var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                var arr2 =['55','2','3','0','21','132'];

                function datesort(arr)
                return arr.concat().sort((a,b)=>
                a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                return new Date(a) - new Date(b)
                )


                var after_arr1 =new datesort(arr1);
                var after_arr2 = arr1.reduce(function(a,b,c)
                var ind = after_arr1.indexOf(b);
                a[ind] = arr2[c]
                return a
                ,[]);


                console.log(after_arr1.join(','));
                console.log(after_arr2.join(','))








                share|improve this answer




















                • 1





                  Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

                  – Amir Qureshi
                  Mar 8 at 10:48






                • 1





                  i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

                  – prasanth
                  Mar 8 at 10:53






                • 1





                  Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

                  – prasanth
                  Mar 8 at 10:56







                • 1





                  i have updated the question please have a look .

                  – Amir Qureshi
                  Mar 8 at 11:01






                • 2





                  you need to sort array2 based on array1 sorted index position right?

                  – prasanth
                  Mar 8 at 11:17















                13














                You need change the string to new Date(dateString) format like



                new Date(Month Date Year)


                Updated regex Pattern for Both Array



                https://regex101.com/r/h1cm1z/2/



                Updated Sort Second array based on first array sorting index






                var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                var arr2 =['55','2','3','0','21','132'];

                function datesort(arr)
                return arr.concat().sort((a,b)=>
                a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                return new Date(a) - new Date(b)
                )


                var after_arr1 =new datesort(arr1);
                var after_arr2 = arr1.reduce(function(a,b,c)
                var ind = after_arr1.indexOf(b);
                a[ind] = arr2[c]
                return a
                ,[]);


                console.log(after_arr1.join(','));
                console.log(after_arr2.join(','))








                share|improve this answer




















                • 1





                  Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

                  – Amir Qureshi
                  Mar 8 at 10:48






                • 1





                  i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

                  – prasanth
                  Mar 8 at 10:53






                • 1





                  Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

                  – prasanth
                  Mar 8 at 10:56







                • 1





                  i have updated the question please have a look .

                  – Amir Qureshi
                  Mar 8 at 11:01






                • 2





                  you need to sort array2 based on array1 sorted index position right?

                  – prasanth
                  Mar 8 at 11:17













                13












                13








                13







                You need change the string to new Date(dateString) format like



                new Date(Month Date Year)


                Updated regex Pattern for Both Array



                https://regex101.com/r/h1cm1z/2/



                Updated Sort Second array based on first array sorting index






                var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                var arr2 =['55','2','3','0','21','132'];

                function datesort(arr)
                return arr.concat().sort((a,b)=>
                a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                return new Date(a) - new Date(b)
                )


                var after_arr1 =new datesort(arr1);
                var after_arr2 = arr1.reduce(function(a,b,c)
                var ind = after_arr1.indexOf(b);
                a[ind] = arr2[c]
                return a
                ,[]);


                console.log(after_arr1.join(','));
                console.log(after_arr2.join(','))








                share|improve this answer















                You need change the string to new Date(dateString) format like



                new Date(Month Date Year)


                Updated regex Pattern for Both Array



                https://regex101.com/r/h1cm1z/2/



                Updated Sort Second array based on first array sorting index






                var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                var arr2 =['55','2','3','0','21','132'];

                function datesort(arr)
                return arr.concat().sort((a,b)=>
                a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                return new Date(a) - new Date(b)
                )


                var after_arr1 =new datesort(arr1);
                var after_arr2 = arr1.reduce(function(a,b,c)
                var ind = after_arr1.indexOf(b);
                a[ind] = arr2[c]
                return a
                ,[]);


                console.log(after_arr1.join(','));
                console.log(after_arr2.join(','))








                var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                var arr2 =['55','2','3','0','21','132'];

                function datesort(arr)
                return arr.concat().sort((a,b)=>
                a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                return new Date(a) - new Date(b)
                )


                var after_arr1 =new datesort(arr1);
                var after_arr2 = arr1.reduce(function(a,b,c)
                var ind = after_arr1.indexOf(b);
                a[ind] = arr2[c]
                return a
                ,[]);


                console.log(after_arr1.join(','));
                console.log(after_arr2.join(','))





                var arr1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                var arr2 =['55','2','3','0','21','132'];

                function datesort(arr)
                return arr.concat().sort((a,b)=>
                a = a.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                b = b.replace(/(d+)(.*)/g,' 1 $1'); // Month 1 YEAR
                return new Date(a) - new Date(b)
                )


                var after_arr1 =new datesort(arr1);
                var after_arr2 = arr1.reduce(function(a,b,c)
                var ind = after_arr1.indexOf(b);
                a[ind] = arr2[c]
                return a
                ,[]);


                console.log(after_arr1.join(','));
                console.log(after_arr2.join(','))






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 16 at 17:08

























                answered Mar 8 at 10:41









                prasanthprasanth

                14.6k21437




                14.6k21437







                • 1





                  Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

                  – Amir Qureshi
                  Mar 8 at 10:48






                • 1





                  i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

                  – prasanth
                  Mar 8 at 10:53






                • 1





                  Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

                  – prasanth
                  Mar 8 at 10:56







                • 1





                  i have updated the question please have a look .

                  – Amir Qureshi
                  Mar 8 at 11:01






                • 2





                  you need to sort array2 based on array1 sorted index position right?

                  – prasanth
                  Mar 8 at 11:17












                • 1





                  Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

                  – Amir Qureshi
                  Mar 8 at 10:48






                • 1





                  i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

                  – prasanth
                  Mar 8 at 10:53






                • 1





                  Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

                  – prasanth
                  Mar 8 at 10:56







                • 1





                  i have updated the question please have a look .

                  – Amir Qureshi
                  Mar 8 at 11:01






                • 2





                  you need to sort array2 based on array1 sorted index position right?

                  – prasanth
                  Mar 8 at 11:17







                1




                1





                Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

                – Amir Qureshi
                Mar 8 at 10:48





                Thank you , it works as required. What if i have another array with values for each month respectively according to the monthyear array. how do i sort it when the monthyear moves to a new position. do you understand what i am saying?

                – Amir Qureshi
                Mar 8 at 10:48




                1




                1





                i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

                – prasanth
                Mar 8 at 10:53





                i could added the format of date .you could change your array value like this.If you have any new position .Kindly change regex pattern and reproduce the same date format.Or post your new position. i will give the matching pattern

                – prasanth
                Mar 8 at 10:53




                1




                1





                Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

                – prasanth
                Mar 8 at 10:56






                Or where did you get this array? You could format array with in pattern on the place of array create.or with something added delimiters like MM_YY,YY-MM

                – prasanth
                Mar 8 at 10:56





                1




                1





                i have updated the question please have a look .

                – Amir Qureshi
                Mar 8 at 11:01





                i have updated the question please have a look .

                – Amir Qureshi
                Mar 8 at 11:01




                2




                2





                you need to sort array2 based on array1 sorted index position right?

                – prasanth
                Mar 8 at 11:17





                you need to sort array2 based on array1 sorted index position right?

                – prasanth
                Mar 8 at 11:17











                10














                To order properly you need to know the order of the months. This is not alphabetical so you can use an array with the order of the months then look them up.



                const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']



                Is already properly sorted and can be used in conjunction with .indexOf() to get the position of a month.






                const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
                ,'Jan19_value'];

                const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                let sortYearMonth = (a, b) =>
                let monthA = monthOrder.indexOf(a.slice(0,3))
                let yearA = a.slice(3,6)
                let monthB = monthOrder.indexOf(b.slice(0,3))
                let yearB = b.slice(3,6)
                return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                let sortedMonths = myArr.sort(sortYearMonth)
                let sortedMonths2 = myArr2.sort(sortYearMonth)

                console.log(sortedMonths )
                console.log(sortedMonths2 )





                Update: values in same position



                Updated version links the two arrays together then sorts the first while keeping the relative position to the second.



                Idea: Link two arrays with a temporary Object then extract the key/value pair using Object.entries. Then sorting the array based on the first value of the pair, which is the value of array1. Then it returns the key/value pair in the right order you can extract the values into two arrays again by using .map()



                I added a run with the string based example and the real values examples below




                const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

                const myArr3 = ['55','2','3','0','21','132'];

                const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                let sortYearMonth = (a, b) =>
                let monthA = monthOrder.indexOf(a.slice(0, 3))
                let yearA = a.slice(3, 6)
                let monthB = monthOrder.indexOf(b.slice(0, 3))
                let yearB = b.slice(3, 6)
                return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                function sortByFirst(myArr, myArr2)
                let keyValue = myArr.reduce((links, item, i) =>
                links[item] = myArr2[i];
                return links
                , )
                let entries = Object.entries(keyValue)
                return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


                let sortedEntries = sortByFirst(myArr, myArr2)
                let sortedMonths = sortedEntries.map(i => i[0])
                let sortedValues = sortedEntries.map(i => i[1])

                let sortedEntries2 = sortByFirst(myArr, myArr3)
                let sortedMonths2 = sortedEntries2.map(i => i[0])
                let sortedValues2 = sortedEntries2.map(i => i[1])

                console.log(sortedMonths)
                console.log(sortedValues)

                console.log(sortedMonths2)
                console.log(sortedValues2)








                share|improve this answer





























                  10














                  To order properly you need to know the order of the months. This is not alphabetical so you can use an array with the order of the months then look them up.



                  const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']



                  Is already properly sorted and can be used in conjunction with .indexOf() to get the position of a month.






                  const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                  const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
                  ,'Jan19_value'];

                  const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                  let sortYearMonth = (a, b) =>
                  let monthA = monthOrder.indexOf(a.slice(0,3))
                  let yearA = a.slice(3,6)
                  let monthB = monthOrder.indexOf(b.slice(0,3))
                  let yearB = b.slice(3,6)
                  return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                  let sortedMonths = myArr.sort(sortYearMonth)
                  let sortedMonths2 = myArr2.sort(sortYearMonth)

                  console.log(sortedMonths )
                  console.log(sortedMonths2 )





                  Update: values in same position



                  Updated version links the two arrays together then sorts the first while keeping the relative position to the second.



                  Idea: Link two arrays with a temporary Object then extract the key/value pair using Object.entries. Then sorting the array based on the first value of the pair, which is the value of array1. Then it returns the key/value pair in the right order you can extract the values into two arrays again by using .map()



                  I added a run with the string based example and the real values examples below




                  const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                  const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

                  const myArr3 = ['55','2','3','0','21','132'];

                  const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                  let sortYearMonth = (a, b) =>
                  let monthA = monthOrder.indexOf(a.slice(0, 3))
                  let yearA = a.slice(3, 6)
                  let monthB = monthOrder.indexOf(b.slice(0, 3))
                  let yearB = b.slice(3, 6)
                  return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                  function sortByFirst(myArr, myArr2)
                  let keyValue = myArr.reduce((links, item, i) =>
                  links[item] = myArr2[i];
                  return links
                  , )
                  let entries = Object.entries(keyValue)
                  return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


                  let sortedEntries = sortByFirst(myArr, myArr2)
                  let sortedMonths = sortedEntries.map(i => i[0])
                  let sortedValues = sortedEntries.map(i => i[1])

                  let sortedEntries2 = sortByFirst(myArr, myArr3)
                  let sortedMonths2 = sortedEntries2.map(i => i[0])
                  let sortedValues2 = sortedEntries2.map(i => i[1])

                  console.log(sortedMonths)
                  console.log(sortedValues)

                  console.log(sortedMonths2)
                  console.log(sortedValues2)








                  share|improve this answer



























                    10












                    10








                    10







                    To order properly you need to know the order of the months. This is not alphabetical so you can use an array with the order of the months then look them up.



                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']



                    Is already properly sorted and can be used in conjunction with .indexOf() to get the position of a month.






                    const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                    const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
                    ,'Jan19_value'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0,3))
                    let yearA = a.slice(3,6)
                    let monthB = monthOrder.indexOf(b.slice(0,3))
                    let yearB = b.slice(3,6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    let sortedMonths = myArr.sort(sortYearMonth)
                    let sortedMonths2 = myArr2.sort(sortYearMonth)

                    console.log(sortedMonths )
                    console.log(sortedMonths2 )





                    Update: values in same position



                    Updated version links the two arrays together then sorts the first while keeping the relative position to the second.



                    Idea: Link two arrays with a temporary Object then extract the key/value pair using Object.entries. Then sorting the array based on the first value of the pair, which is the value of array1. Then it returns the key/value pair in the right order you can extract the values into two arrays again by using .map()



                    I added a run with the string based example and the real values examples below




                    const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                    const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

                    const myArr3 = ['55','2','3','0','21','132'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0, 3))
                    let yearA = a.slice(3, 6)
                    let monthB = monthOrder.indexOf(b.slice(0, 3))
                    let yearB = b.slice(3, 6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    function sortByFirst(myArr, myArr2)
                    let keyValue = myArr.reduce((links, item, i) =>
                    links[item] = myArr2[i];
                    return links
                    , )
                    let entries = Object.entries(keyValue)
                    return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


                    let sortedEntries = sortByFirst(myArr, myArr2)
                    let sortedMonths = sortedEntries.map(i => i[0])
                    let sortedValues = sortedEntries.map(i => i[1])

                    let sortedEntries2 = sortByFirst(myArr, myArr3)
                    let sortedMonths2 = sortedEntries2.map(i => i[0])
                    let sortedValues2 = sortedEntries2.map(i => i[1])

                    console.log(sortedMonths)
                    console.log(sortedValues)

                    console.log(sortedMonths2)
                    console.log(sortedValues2)








                    share|improve this answer















                    To order properly you need to know the order of the months. This is not alphabetical so you can use an array with the order of the months then look them up.



                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']



                    Is already properly sorted and can be used in conjunction with .indexOf() to get the position of a month.






                    const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                    const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
                    ,'Jan19_value'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0,3))
                    let yearA = a.slice(3,6)
                    let monthB = monthOrder.indexOf(b.slice(0,3))
                    let yearB = b.slice(3,6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    let sortedMonths = myArr.sort(sortYearMonth)
                    let sortedMonths2 = myArr2.sort(sortYearMonth)

                    console.log(sortedMonths )
                    console.log(sortedMonths2 )





                    Update: values in same position



                    Updated version links the two arrays together then sorts the first while keeping the relative position to the second.



                    Idea: Link two arrays with a temporary Object then extract the key/value pair using Object.entries. Then sorting the array based on the first value of the pair, which is the value of array1. Then it returns the key/value pair in the right order you can extract the values into two arrays again by using .map()



                    I added a run with the string based example and the real values examples below




                    const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                    const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

                    const myArr3 = ['55','2','3','0','21','132'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0, 3))
                    let yearA = a.slice(3, 6)
                    let monthB = monthOrder.indexOf(b.slice(0, 3))
                    let yearB = b.slice(3, 6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    function sortByFirst(myArr, myArr2)
                    let keyValue = myArr.reduce((links, item, i) =>
                    links[item] = myArr2[i];
                    return links
                    , )
                    let entries = Object.entries(keyValue)
                    return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


                    let sortedEntries = sortByFirst(myArr, myArr2)
                    let sortedMonths = sortedEntries.map(i => i[0])
                    let sortedValues = sortedEntries.map(i => i[1])

                    let sortedEntries2 = sortByFirst(myArr, myArr3)
                    let sortedMonths2 = sortedEntries2.map(i => i[0])
                    let sortedValues2 = sortedEntries2.map(i => i[1])

                    console.log(sortedMonths)
                    console.log(sortedValues)

                    console.log(sortedMonths2)
                    console.log(sortedValues2)








                    const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                    const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
                    ,'Jan19_value'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0,3))
                    let yearA = a.slice(3,6)
                    let monthB = monthOrder.indexOf(b.slice(0,3))
                    let yearB = b.slice(3,6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    let sortedMonths = myArr.sort(sortYearMonth)
                    let sortedMonths2 = myArr2.sort(sortYearMonth)

                    console.log(sortedMonths )
                    console.log(sortedMonths2 )





                    const myArr = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                    const myArr2 = ['Mar19_value','Apr18_value','Jun18_value','Jul18_value','May18_value'
                    ,'Jan19_value'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0,3))
                    let yearA = a.slice(3,6)
                    let monthB = monthOrder.indexOf(b.slice(0,3))
                    let yearB = b.slice(3,6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    let sortedMonths = myArr.sort(sortYearMonth)
                    let sortedMonths2 = myArr2.sort(sortYearMonth)

                    console.log(sortedMonths )
                    console.log(sortedMonths2 )





                    const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                    const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

                    const myArr3 = ['55','2','3','0','21','132'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0, 3))
                    let yearA = a.slice(3, 6)
                    let monthB = monthOrder.indexOf(b.slice(0, 3))
                    let yearB = b.slice(3, 6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    function sortByFirst(myArr, myArr2)
                    let keyValue = myArr.reduce((links, item, i) =>
                    links[item] = myArr2[i];
                    return links
                    , )
                    let entries = Object.entries(keyValue)
                    return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


                    let sortedEntries = sortByFirst(myArr, myArr2)
                    let sortedMonths = sortedEntries.map(i => i[0])
                    let sortedValues = sortedEntries.map(i => i[1])

                    let sortedEntries2 = sortByFirst(myArr, myArr3)
                    let sortedMonths2 = sortedEntries2.map(i => i[0])
                    let sortedValues2 = sortedEntries2.map(i => i[1])

                    console.log(sortedMonths)
                    console.log(sortedValues)

                    console.log(sortedMonths2)
                    console.log(sortedValues2)





                    const myArr = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                    const myArr2 = ['Mar19_value', 'Apr18_value', 'Jun18_value', 'Jul18_value', 'May18_value', 'Jan19_value'];

                    const myArr3 = ['55','2','3','0','21','132'];

                    const monthOrder = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

                    let sortYearMonth = (a, b) =>
                    let monthA = monthOrder.indexOf(a.slice(0, 3))
                    let yearA = a.slice(3, 6)
                    let monthB = monthOrder.indexOf(b.slice(0, 3))
                    let yearB = b.slice(3, 6)
                    return (`$yearA-$monthA` < `$yearB-$monthB`) ? -1 : (`$yearA-$monthA` > `$yearB-$monthB`) ? 1 : 0


                    function sortByFirst(myArr, myArr2)
                    let keyValue = myArr.reduce((links, item, i) =>
                    links[item] = myArr2[i];
                    return links
                    , )
                    let entries = Object.entries(keyValue)
                    return entries.sort((a, b) => sortYearMonth(a[0], b[0]))


                    let sortedEntries = sortByFirst(myArr, myArr2)
                    let sortedMonths = sortedEntries.map(i => i[0])
                    let sortedValues = sortedEntries.map(i => i[1])

                    let sortedEntries2 = sortByFirst(myArr, myArr3)
                    let sortedMonths2 = sortedEntries2.map(i => i[0])
                    let sortedValues2 = sortedEntries2.map(i => i[1])

                    console.log(sortedMonths)
                    console.log(sortedValues)

                    console.log(sortedMonths2)
                    console.log(sortedValues2)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 8 at 11:43

























                    answered Mar 8 at 10:51









                    Jordan MaduroJordan Maduro

                    593311




                    593311





















                        10

















                        const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

                        //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
                        //Can be done easily by using momentjs, darte-fns, but here i will do it natively

                        const t =
                        Jan: 1,
                        Feb: 2,
                        Mar: 3,
                        Apr: 4,
                        May: 5,
                        Jun: 6,
                        Jul: 7,
                        Aug: 8,
                        Sep: 9,
                        Oct: 10,
                        Nov: 11,
                        Dec: 12

                        const giveConcatString = (a, t) =>
                        const monthPart = a.substr(0, 3)
                        const yearPart = a.substr(3)
                        return `$yearPart$t[monthPart]`

                        const sortedArray = input.sort((a, b) =>
                        const concatString = giveConcatString(a, t)
                        const concatStringB = giveConcatString(b, t)
                        return concatString <= concatStringB ? -1 : 1

                        )
                        console.log(sortedArray)





                        This may help you solve this problem. Did it natively.






                        share|improve this answer





























                          10

















                          const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

                          //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
                          //Can be done easily by using momentjs, darte-fns, but here i will do it natively

                          const t =
                          Jan: 1,
                          Feb: 2,
                          Mar: 3,
                          Apr: 4,
                          May: 5,
                          Jun: 6,
                          Jul: 7,
                          Aug: 8,
                          Sep: 9,
                          Oct: 10,
                          Nov: 11,
                          Dec: 12

                          const giveConcatString = (a, t) =>
                          const monthPart = a.substr(0, 3)
                          const yearPart = a.substr(3)
                          return `$yearPart$t[monthPart]`

                          const sortedArray = input.sort((a, b) =>
                          const concatString = giveConcatString(a, t)
                          const concatStringB = giveConcatString(b, t)
                          return concatString <= concatStringB ? -1 : 1

                          )
                          console.log(sortedArray)





                          This may help you solve this problem. Did it natively.






                          share|improve this answer



























                            10












                            10








                            10










                            const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

                            //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
                            //Can be done easily by using momentjs, darte-fns, but here i will do it natively

                            const t =
                            Jan: 1,
                            Feb: 2,
                            Mar: 3,
                            Apr: 4,
                            May: 5,
                            Jun: 6,
                            Jul: 7,
                            Aug: 8,
                            Sep: 9,
                            Oct: 10,
                            Nov: 11,
                            Dec: 12

                            const giveConcatString = (a, t) =>
                            const monthPart = a.substr(0, 3)
                            const yearPart = a.substr(3)
                            return `$yearPart$t[monthPart]`

                            const sortedArray = input.sort((a, b) =>
                            const concatString = giveConcatString(a, t)
                            const concatStringB = giveConcatString(b, t)
                            return concatString <= concatStringB ? -1 : 1

                            )
                            console.log(sortedArray)





                            This may help you solve this problem. Did it natively.






                            share|improve this answer


















                            const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

                            //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
                            //Can be done easily by using momentjs, darte-fns, but here i will do it natively

                            const t =
                            Jan: 1,
                            Feb: 2,
                            Mar: 3,
                            Apr: 4,
                            May: 5,
                            Jun: 6,
                            Jul: 7,
                            Aug: 8,
                            Sep: 9,
                            Oct: 10,
                            Nov: 11,
                            Dec: 12

                            const giveConcatString = (a, t) =>
                            const monthPart = a.substr(0, 3)
                            const yearPart = a.substr(3)
                            return `$yearPart$t[monthPart]`

                            const sortedArray = input.sort((a, b) =>
                            const concatString = giveConcatString(a, t)
                            const concatStringB = giveConcatString(b, t)
                            return concatString <= concatStringB ? -1 : 1

                            )
                            console.log(sortedArray)





                            This may help you solve this problem. Did it natively.






                            const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

                            //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
                            //Can be done easily by using momentjs, darte-fns, but here i will do it natively

                            const t =
                            Jan: 1,
                            Feb: 2,
                            Mar: 3,
                            Apr: 4,
                            May: 5,
                            Jun: 6,
                            Jul: 7,
                            Aug: 8,
                            Sep: 9,
                            Oct: 10,
                            Nov: 11,
                            Dec: 12

                            const giveConcatString = (a, t) =>
                            const monthPart = a.substr(0, 3)
                            const yearPart = a.substr(3)
                            return `$yearPart$t[monthPart]`

                            const sortedArray = input.sort((a, b) =>
                            const concatString = giveConcatString(a, t)
                            const concatStringB = giveConcatString(b, t)
                            return concatString <= concatStringB ? -1 : 1

                            )
                            console.log(sortedArray)





                            const input = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];

                            //const output : ['Apr18','May18','Jun18','Jul18','Jan19','Mar19'];
                            //Can be done easily by using momentjs, darte-fns, but here i will do it natively

                            const t =
                            Jan: 1,
                            Feb: 2,
                            Mar: 3,
                            Apr: 4,
                            May: 5,
                            Jun: 6,
                            Jul: 7,
                            Aug: 8,
                            Sep: 9,
                            Oct: 10,
                            Nov: 11,
                            Dec: 12

                            const giveConcatString = (a, t) =>
                            const monthPart = a.substr(0, 3)
                            const yearPart = a.substr(3)
                            return `$yearPart$t[monthPart]`

                            const sortedArray = input.sort((a, b) =>
                            const concatString = giveConcatString(a, t)
                            const concatStringB = giveConcatString(b, t)
                            return concatString <= concatStringB ? -1 : 1

                            )
                            console.log(sortedArray)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 8 at 12:16

























                            answered Mar 8 at 10:42









                            simbathesailorsimbathesailor

                            2,28011119




                            2,28011119





















                                4














                                You can try this raw snippet.






                                var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                                var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
                                // converting to raw date format
                                for (i in MY)
                                num = MY[i].match(/d+/g);
                                letr = MY[i].match(/[a-zA-Z]+/g);
                                MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

                                // sorting logic
                                var sorted = MY.sort(function(a, b)
                                a = a.split("-");
                                b = b.split("-")
                                return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
                                );
                                // converting back to original array after sorting
                                res = [];
                                for (i in sorted)
                                var a = sorted[i].split('-');
                                res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


                                console.log(res);








                                share|improve this answer



























                                  4














                                  You can try this raw snippet.






                                  var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                                  var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
                                  // converting to raw date format
                                  for (i in MY)
                                  num = MY[i].match(/d+/g);
                                  letr = MY[i].match(/[a-zA-Z]+/g);
                                  MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

                                  // sorting logic
                                  var sorted = MY.sort(function(a, b)
                                  a = a.split("-");
                                  b = b.split("-")
                                  return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
                                  );
                                  // converting back to original array after sorting
                                  res = [];
                                  for (i in sorted)
                                  var a = sorted[i].split('-');
                                  res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


                                  console.log(res);








                                  share|improve this answer

























                                    4












                                    4








                                    4







                                    You can try this raw snippet.






                                    var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                                    var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
                                    // converting to raw date format
                                    for (i in MY)
                                    num = MY[i].match(/d+/g);
                                    letr = MY[i].match(/[a-zA-Z]+/g);
                                    MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

                                    // sorting logic
                                    var sorted = MY.sort(function(a, b)
                                    a = a.split("-");
                                    b = b.split("-")
                                    return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
                                    );
                                    // converting back to original array after sorting
                                    res = [];
                                    for (i in sorted)
                                    var a = sorted[i].split('-');
                                    res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


                                    console.log(res);








                                    share|improve this answer













                                    You can try this raw snippet.






                                    var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                                    var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
                                    // converting to raw date format
                                    for (i in MY)
                                    num = MY[i].match(/d+/g);
                                    letr = MY[i].match(/[a-zA-Z]+/g);
                                    MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

                                    // sorting logic
                                    var sorted = MY.sort(function(a, b)
                                    a = a.split("-");
                                    b = b.split("-")
                                    return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
                                    );
                                    // converting back to original array after sorting
                                    res = [];
                                    for (i in sorted)
                                    var a = sorted[i].split('-');
                                    res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


                                    console.log(res);








                                    var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                                    var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
                                    // converting to raw date format
                                    for (i in MY)
                                    num = MY[i].match(/d+/g);
                                    letr = MY[i].match(/[a-zA-Z]+/g);
                                    MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

                                    // sorting logic
                                    var sorted = MY.sort(function(a, b)
                                    a = a.split("-");
                                    b = b.split("-")
                                    return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
                                    );
                                    // converting back to original array after sorting
                                    res = [];
                                    for (i in sorted)
                                    var a = sorted[i].split('-');
                                    res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


                                    console.log(res);





                                    var MY = ['Mar19', 'Apr18', 'Jun18', 'Jul18', 'May18', 'Jan19'];
                                    var s = "JanFebMarAprMayJunJulAugSepOctNovDec";
                                    // converting to raw date format
                                    for (i in MY)
                                    num = MY[i].match(/d+/g);
                                    letr = MY[i].match(/[a-zA-Z]+/g);
                                    MY[i] = (s.indexOf(letr) / 3 + 1) + '-' + '20' + num;

                                    // sorting logic
                                    var sorted = MY.sort(function(a, b)
                                    a = a.split("-");
                                    b = b.split("-")
                                    return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
                                    );
                                    // converting back to original array after sorting
                                    res = [];
                                    for (i in sorted)
                                    var a = sorted[i].split('-');
                                    res[i] = (s.substr((parseInt(a[0]) - 1) * 3, 3)) + '-' + a[1].substr(2, 2);


                                    console.log(res);






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 8 at 11:01









                                    Rahul MeshramRahul Meshram

                                    7,54142042




                                    7,54142042





















                                        3














                                        I used lodash for sorting and substring method to divide date into month and day parts






                                        const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

                                        const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                                        const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

                                        console.log(sorted)

                                        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                                        share|improve this answer



























                                          3














                                          I used lodash for sorting and substring method to divide date into month and day parts






                                          const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

                                          const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                                          const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

                                          console.log(sorted)

                                          <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                                          share|improve this answer

























                                            3












                                            3








                                            3







                                            I used lodash for sorting and substring method to divide date into month and day parts






                                            const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

                                            const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                                            const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

                                            console.log(sorted)

                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                                            share|improve this answer













                                            I used lodash for sorting and substring method to divide date into month and day parts






                                            const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

                                            const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                                            const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

                                            console.log(sorted)

                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








                                            const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

                                            const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                                            const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

                                            console.log(sorted)

                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>





                                            const data = ['Mar20','Mar21', 'Mar01', 'Mar19','Apr18','Jun18','Jul18','May18','Jan19']

                                            const monthMap = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                                            const sorted = _.sortBy(data, date => monthMap.indexOf(date.substring(0,3)), date => date.substring(3,5))

                                            console.log(sorted)

                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Mar 8 at 10:46









                                            Krzysztof KrzeszewskiKrzysztof Krzeszewski

                                            8341210




                                            8341210





















                                                3














                                                let mth = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];

                                                mth.sort((itemA, itemB)=>
                                                let mthAstr = itemA.slice(0,3)+" 01 "+itemA.slice(3,5);
                                                let mthA = new Date(mthAstr);
                                                let mthBstr = itemB.slice(0,3)+" 01 "+itemB.slice(3,5);
                                                let mthB = new Date(mthBstr);
                                                if (mthA < mthB)
                                                return -1;
                                                if (mthA > mthB)
                                                return 1;
                                                return 0;
                                                );





                                                share|improve this answer



























                                                  3














                                                  let mth = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];

                                                  mth.sort((itemA, itemB)=>
                                                  let mthAstr = itemA.slice(0,3)+" 01 "+itemA.slice(3,5);
                                                  let mthA = new Date(mthAstr);
                                                  let mthBstr = itemB.slice(0,3)+" 01 "+itemB.slice(3,5);
                                                  let mthB = new Date(mthBstr);
                                                  if (mthA < mthB)
                                                  return -1;
                                                  if (mthA > mthB)
                                                  return 1;
                                                  return 0;
                                                  );





                                                  share|improve this answer

























                                                    3












                                                    3








                                                    3







                                                    let mth = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];

                                                    mth.sort((itemA, itemB)=>
                                                    let mthAstr = itemA.slice(0,3)+" 01 "+itemA.slice(3,5);
                                                    let mthA = new Date(mthAstr);
                                                    let mthBstr = itemB.slice(0,3)+" 01 "+itemB.slice(3,5);
                                                    let mthB = new Date(mthBstr);
                                                    if (mthA < mthB)
                                                    return -1;
                                                    if (mthA > mthB)
                                                    return 1;
                                                    return 0;
                                                    );





                                                    share|improve this answer













                                                    let mth = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];

                                                    mth.sort((itemA, itemB)=>
                                                    let mthAstr = itemA.slice(0,3)+" 01 "+itemA.slice(3,5);
                                                    let mthA = new Date(mthAstr);
                                                    let mthBstr = itemB.slice(0,3)+" 01 "+itemB.slice(3,5);
                                                    let mthB = new Date(mthBstr);
                                                    if (mthA < mthB)
                                                    return -1;
                                                    if (mthA > mthB)
                                                    return 1;
                                                    return 0;
                                                    );






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Mar 8 at 10:58









                                                    EdperEdper

                                                    7,27712042




                                                    7,27712042





















                                                        3














                                                        You can try something like that :



                                                        var Array = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                        Array.sort(function(a, b)
                                                        a = [a.slice(0,3), ' 20', a.slice(3)].join('');
                                                        b = [b.slice(0,3), ' 20', b.slice(3)].join('')
                                                        return new Date() - new Date(b);
                                                        );

                                                        console.log(Array);





                                                        share|improve this answer





























                                                          3














                                                          You can try something like that :



                                                          var Array = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                          Array.sort(function(a, b)
                                                          a = [a.slice(0,3), ' 20', a.slice(3)].join('');
                                                          b = [b.slice(0,3), ' 20', b.slice(3)].join('')
                                                          return new Date() - new Date(b);
                                                          );

                                                          console.log(Array);





                                                          share|improve this answer



























                                                            3












                                                            3








                                                            3







                                                            You can try something like that :



                                                            var Array = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                            Array.sort(function(a, b)
                                                            a = [a.slice(0,3), ' 20', a.slice(3)].join('');
                                                            b = [b.slice(0,3), ' 20', b.slice(3)].join('')
                                                            return new Date() - new Date(b);
                                                            );

                                                            console.log(Array);





                                                            share|improve this answer















                                                            You can try something like that :



                                                            var Array = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                            Array.sort(function(a, b)
                                                            a = [a.slice(0,3), ' 20', a.slice(3)].join('');
                                                            b = [b.slice(0,3), ' 20', b.slice(3)].join('')
                                                            return new Date() - new Date(b);
                                                            );

                                                            console.log(Array);






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Mar 8 at 12:28









                                                            Enzo BLANCHON

                                                            756317




                                                            756317










                                                            answered Mar 8 at 11:02









                                                            KAMLESH NEHRAKAMLESH NEHRA

                                                            312




                                                            312





















                                                                1














                                                                I would suggest that your data structure is unfortunate. Working with shared indices makes it much more difficult than a data structure that combines them, such as an array of objects, [label: 'Mar19', value: 55, ...].



                                                                If your data is coming from an upstream solution you cannot control, you can still manage this in your own work, converting before you use it. (And if you really have to converting back to pass to others.)



                                                                A common name for a function combining two arrays is zip -- think of it acting like a zipper. This version uses one that takes a function to say how the paired elements should be combined. (Elsewhere such a function might be called zipWith.)



                                                                Here sortArraysByDate calls zip passing a function that turns 'Mar19' and 55 into label: 'Mar19, value: 55, month: 'Mar', year: 19 using dateFields to extract the month and year from that label, then sorts these using the straightforward dateSort






                                                                const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
                                                                Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

                                                                const dateFields = (d) => (
                                                                month: d.slice(0, 3),
                                                                year: Number(d.slice(3))
                                                                )

                                                                const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
                                                                (y1 - y2) || (months[m1] - months[m2])

                                                                const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

                                                                const sortArraysByDate = (a1, a2) =>
                                                                zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
                                                                .sort(dateSort)
                                                                .map((label, value) => (label, value))


                                                                const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                                const Array2 = ['55','2','3','0','21','132']; //real values

                                                                const result = sortArraysByDate(Array1, Array2)

                                                                console.log(result)





                                                                The map call in sortArraysByDate is quite possibly not necessary. Without it, the resulting data includes extra year and month fields; that might not be an issue.



                                                                If you really need those updated arrays in the original format, you can just map the result:



                                                                const newArray1 = result.map(o => o.label)
                                                                const newArray2 = result.map(o => o.value)


                                                                But I would urge you not to do this unless it's absolutely necessary. This structure is really useful. The paired arrays are much less so.



                                                                Also, if you need to combine more than two arrays, you could write a slightly more sophisticated version of zip:



                                                                const zip = (fn, ...as) => as[0].map((_, i) => fn(...as.map(a => a[i])))


                                                                This takes a function on n arguments, and n arrays, and yields a new array containing the result of calling that function respectively on the successive items in each of the arrays.






                                                                share|improve this answer





























                                                                  1














                                                                  I would suggest that your data structure is unfortunate. Working with shared indices makes it much more difficult than a data structure that combines them, such as an array of objects, [label: 'Mar19', value: 55, ...].



                                                                  If your data is coming from an upstream solution you cannot control, you can still manage this in your own work, converting before you use it. (And if you really have to converting back to pass to others.)



                                                                  A common name for a function combining two arrays is zip -- think of it acting like a zipper. This version uses one that takes a function to say how the paired elements should be combined. (Elsewhere such a function might be called zipWith.)



                                                                  Here sortArraysByDate calls zip passing a function that turns 'Mar19' and 55 into label: 'Mar19, value: 55, month: 'Mar', year: 19 using dateFields to extract the month and year from that label, then sorts these using the straightforward dateSort






                                                                  const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
                                                                  Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

                                                                  const dateFields = (d) => (
                                                                  month: d.slice(0, 3),
                                                                  year: Number(d.slice(3))
                                                                  )

                                                                  const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
                                                                  (y1 - y2) || (months[m1] - months[m2])

                                                                  const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

                                                                  const sortArraysByDate = (a1, a2) =>
                                                                  zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
                                                                  .sort(dateSort)
                                                                  .map((label, value) => (label, value))


                                                                  const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                                  const Array2 = ['55','2','3','0','21','132']; //real values

                                                                  const result = sortArraysByDate(Array1, Array2)

                                                                  console.log(result)





                                                                  The map call in sortArraysByDate is quite possibly not necessary. Without it, the resulting data includes extra year and month fields; that might not be an issue.



                                                                  If you really need those updated arrays in the original format, you can just map the result:



                                                                  const newArray1 = result.map(o => o.label)
                                                                  const newArray2 = result.map(o => o.value)


                                                                  But I would urge you not to do this unless it's absolutely necessary. This structure is really useful. The paired arrays are much less so.



                                                                  Also, if you need to combine more than two arrays, you could write a slightly more sophisticated version of zip:



                                                                  const zip = (fn, ...as) => as[0].map((_, i) => fn(...as.map(a => a[i])))


                                                                  This takes a function on n arguments, and n arrays, and yields a new array containing the result of calling that function respectively on the successive items in each of the arrays.






                                                                  share|improve this answer



























                                                                    1












                                                                    1








                                                                    1







                                                                    I would suggest that your data structure is unfortunate. Working with shared indices makes it much more difficult than a data structure that combines them, such as an array of objects, [label: 'Mar19', value: 55, ...].



                                                                    If your data is coming from an upstream solution you cannot control, you can still manage this in your own work, converting before you use it. (And if you really have to converting back to pass to others.)



                                                                    A common name for a function combining two arrays is zip -- think of it acting like a zipper. This version uses one that takes a function to say how the paired elements should be combined. (Elsewhere such a function might be called zipWith.)



                                                                    Here sortArraysByDate calls zip passing a function that turns 'Mar19' and 55 into label: 'Mar19, value: 55, month: 'Mar', year: 19 using dateFields to extract the month and year from that label, then sorts these using the straightforward dateSort






                                                                    const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
                                                                    Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

                                                                    const dateFields = (d) => (
                                                                    month: d.slice(0, 3),
                                                                    year: Number(d.slice(3))
                                                                    )

                                                                    const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
                                                                    (y1 - y2) || (months[m1] - months[m2])

                                                                    const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

                                                                    const sortArraysByDate = (a1, a2) =>
                                                                    zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
                                                                    .sort(dateSort)
                                                                    .map((label, value) => (label, value))


                                                                    const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                                    const Array2 = ['55','2','3','0','21','132']; //real values

                                                                    const result = sortArraysByDate(Array1, Array2)

                                                                    console.log(result)





                                                                    The map call in sortArraysByDate is quite possibly not necessary. Without it, the resulting data includes extra year and month fields; that might not be an issue.



                                                                    If you really need those updated arrays in the original format, you can just map the result:



                                                                    const newArray1 = result.map(o => o.label)
                                                                    const newArray2 = result.map(o => o.value)


                                                                    But I would urge you not to do this unless it's absolutely necessary. This structure is really useful. The paired arrays are much less so.



                                                                    Also, if you need to combine more than two arrays, you could write a slightly more sophisticated version of zip:



                                                                    const zip = (fn, ...as) => as[0].map((_, i) => fn(...as.map(a => a[i])))


                                                                    This takes a function on n arguments, and n arrays, and yields a new array containing the result of calling that function respectively on the successive items in each of the arrays.






                                                                    share|improve this answer















                                                                    I would suggest that your data structure is unfortunate. Working with shared indices makes it much more difficult than a data structure that combines them, such as an array of objects, [label: 'Mar19', value: 55, ...].



                                                                    If your data is coming from an upstream solution you cannot control, you can still manage this in your own work, converting before you use it. (And if you really have to converting back to pass to others.)



                                                                    A common name for a function combining two arrays is zip -- think of it acting like a zipper. This version uses one that takes a function to say how the paired elements should be combined. (Elsewhere such a function might be called zipWith.)



                                                                    Here sortArraysByDate calls zip passing a function that turns 'Mar19' and 55 into label: 'Mar19, value: 55, month: 'Mar', year: 19 using dateFields to extract the month and year from that label, then sorts these using the straightforward dateSort






                                                                    const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
                                                                    Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

                                                                    const dateFields = (d) => (
                                                                    month: d.slice(0, 3),
                                                                    year: Number(d.slice(3))
                                                                    )

                                                                    const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
                                                                    (y1 - y2) || (months[m1] - months[m2])

                                                                    const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

                                                                    const sortArraysByDate = (a1, a2) =>
                                                                    zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
                                                                    .sort(dateSort)
                                                                    .map((label, value) => (label, value))


                                                                    const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                                    const Array2 = ['55','2','3','0','21','132']; //real values

                                                                    const result = sortArraysByDate(Array1, Array2)

                                                                    console.log(result)





                                                                    The map call in sortArraysByDate is quite possibly not necessary. Without it, the resulting data includes extra year and month fields; that might not be an issue.



                                                                    If you really need those updated arrays in the original format, you can just map the result:



                                                                    const newArray1 = result.map(o => o.label)
                                                                    const newArray2 = result.map(o => o.value)


                                                                    But I would urge you not to do this unless it's absolutely necessary. This structure is really useful. The paired arrays are much less so.



                                                                    Also, if you need to combine more than two arrays, you could write a slightly more sophisticated version of zip:



                                                                    const zip = (fn, ...as) => as[0].map((_, i) => fn(...as.map(a => a[i])))


                                                                    This takes a function on n arguments, and n arrays, and yields a new array containing the result of calling that function respectively on the successive items in each of the arrays.






                                                                    const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
                                                                    Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

                                                                    const dateFields = (d) => (
                                                                    month: d.slice(0, 3),
                                                                    year: Number(d.slice(3))
                                                                    )

                                                                    const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
                                                                    (y1 - y2) || (months[m1] - months[m2])

                                                                    const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

                                                                    const sortArraysByDate = (a1, a2) =>
                                                                    zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
                                                                    .sort(dateSort)
                                                                    .map((label, value) => (label, value))


                                                                    const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                                    const Array2 = ['55','2','3','0','21','132']; //real values

                                                                    const result = sortArraysByDate(Array1, Array2)

                                                                    console.log(result)





                                                                    const months = Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
                                                                    Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12

                                                                    const dateFields = (d) => (
                                                                    month: d.slice(0, 3),
                                                                    year: Number(d.slice(3))
                                                                    )

                                                                    const dateSort = (month: m1, year: y1, month: m2, year: y2) =>
                                                                    (y1 - y2) || (months[m1] - months[m2])

                                                                    const zip = (fn, a1, a2) => a1.map((a, i) => fn(a, a2[i]))

                                                                    const sortArraysByDate = (a1, a2) =>
                                                                    zip((label, value) => (label, value, ...dateFields(label)), a1, a2)
                                                                    .sort(dateSort)
                                                                    .map((label, value) => (label, value))


                                                                    const Array1 = ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'];
                                                                    const Array2 = ['55','2','3','0','21','132']; //real values

                                                                    const result = sortArraysByDate(Array1, Array2)

                                                                    console.log(result)






                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Mar 8 at 20:41

























                                                                    answered Mar 8 at 19:43









                                                                    Scott SauyetScott Sauyet

                                                                    21.7k22758




                                                                    21.7k22758



























                                                                        draft saved

                                                                        draft discarded
















































                                                                        Thanks for contributing an answer to Stack Overflow!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid


                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.

                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function ()
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061324%2fsort-array-by-month-and-year%23new-answer', 'question_page');

                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                                                                        2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                                                                        Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme