I have an array organized as such.
const array = [
{
Year: 2018,
Month: 'Dec'
},
{
Year: 2017,
Month: 'Apr'
},
{
Year: 2018,
Month: 'Mar'
},
{
Year: 2018,
Month: 'Oct'
},
{
Year: 2017,
Month: 'Jan'
},
{
Year: 2018,
Month: 'Apr'
}
]
I have successfully organized the data by Year or by Month but every time I try to organize it by both of them at once, then whichever one organized the data last will supersede everything before it. I understand why it is doing this but can't seem to find a way around it.
const sortedByYear = array.sort((a, b) => a.Year - b.Year);
Sorts by year quite simply.
const sorted = sortedByYear.sort((a, b) => Months.indexOf(a.Month) - Months.indexOf(b.Month));
Sorts by Month.
I tried to add some sort of checker in the Month checker, if the years matched then resort but that doesn't solve the problem for how it will sort.
I have an array organized as such.
const array = [
{
Year: 2018,
Month: 'Dec'
},
{
Year: 2017,
Month: 'Apr'
},
{
Year: 2018,
Month: 'Mar'
},
{
Year: 2018,
Month: 'Oct'
},
{
Year: 2017,
Month: 'Jan'
},
{
Year: 2018,
Month: 'Apr'
}
]
I have successfully organized the data by Year or by Month but every time I try to organize it by both of them at once, then whichever one organized the data last will supersede everything before it. I understand why it is doing this but can't seem to find a way around it.
const sortedByYear = array.sort((a, b) => a.Year - b.Year);
Sorts by year quite simply.
const sorted = sortedByYear.sort((a, b) => Months.indexOf(a.Month) - Months.indexOf(b.Month));
Sorts by Month.
I tried to add some sort of checker in the Month checker, if the years matched then resort but that doesn't solve the problem for how it will sort.
Share Improve this question asked May 1, 2018 at 22:26 BrandonBrandon 1,6172 gold badges24 silver badges45 bronze badges 2- Sort by year, if they are the same - sort by month. – PM 77-1 Commented May 1, 2018 at 22:29
- This is a fun use case for contravariant functors – I share an answer demonstrating a multi-sort like the one you're trying to achieve here: stackoverflow./a/47640811/633183 – Mulan Commented May 1, 2018 at 22:49
3 Answers
Reset to default 3You'll have to put both tests in your sort
function:
const input = [
{
Year: 2018,
Month: 'Dec'
},
{
Year: 2017,
Month: 'Apr'
},
{
Year: 2018,
Month: 'Mar'
},
{
Year: 2018,
Month: 'Oct'
},
{
Year: 2017,
Month: 'Jan'
},
{
Year: 2018,
Month: 'Apr'
}
];
const Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
input.sort((a, b) => {
if (a.Year !== b.Year) return a.Year - b.Year;
return Months.indexOf(a.Month) - Months.indexOf(b.Month)
});
console.log(input);
You can use lodash sortBy and pass the fields you want to sort by as an array.
const sorted = _.sortBy(array, ['Year', 'Month']);
You can use bination of year
and month
to form a parison criteria. padStart will not work on IE so you may need a polyfill for this.
const input = [
{
Year: 2018,
Month: 'Dec'
},
{
Year: 2017,
Month: 'Apr'
},
{
Year: 2018,
Month: 'Mar'
},
{
Year: 2018,
Month: 'Oct'
},
{
Year: 2017,
Month: 'Jan'
},
{
Year: 2018,
Month: 'Apr'
}
];
const Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
input.sort((a, b) =>`${a.Year}${Months.indexOf(a.Month).toString().padStart(2,0)}` - `${b.Year}${Months.indexOf(b.Month).toString().padStart(2,0)}`
);
console.log(input);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744751950a4591651.html
评论列表(0条)