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?
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(','))
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));
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 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.
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);
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>
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;
});
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With