JSFiddle - React, Tailwind, and code Playground
JavaScript
var data = [
{
type: 'S',
year: 'SW Walker'},
{
type: 'C',
year: 'SW Greth'},
{
type: 'S',
year: 'SW Karp'},
{
type: 'C',
year: 'SW Main'},
{
type: 'H',
year: 'SW Dummy'}
];
data.sort(function(a, b) {
var order = 'SCH';
var typeA = order.indexOf(a.type);
var typeB = order.indexOf(b.type);
if (typeA == typeB) {
if (a.year < b.year) {
return -1;
} else if (a.year == b.year) {
return 0;
} else {
return 1;
}
} else {
return typeA - typeB;
}
});
console.log(data);