I have a set of data that has multiple properties, and the primary way I want them sorted is having item B after item A, but I don't want to affect the order of those items outside of that.
Dataset (in no particular order):
Dataset (sorted by item A)
My goal is to sort whatever has the "item A property" after the actual "item A". I'm blanking on how to make sure it's always sorted below the other item, regardless of what order each item returns.
I'm trying to do this within a JavaScript sort function, if possible.
Here's another example as well:
Dataset (in no particular order):
Dataset (sorted by item A)
You could map the items and for the wanted group take an array. later flat the result.
const
array = ['b', 'c', 'd', 'e', 'aa', 'a'],
group = [],
result = array
.map(v => {
if (v === 'a') { // first item
group.unshift(v);
return group;
}
if (v === 'aa') { // connected item
group.push(v);
return [];
}
return v;
})
.flat();
console.log(...result);