I have Array like this
array = [
{name: '0:homdel', data: Array(3)},
{name: '1:LOH', data: Array(5)},
{name: '2:het', data: Array(5)},
{name: '2:LOH', data: Array(5)},
{name: '3-4:het', data: Array(5)},
{name: '3-4:LOH', data: Array(5)} ,
{name: '5-8:het', data: Array(5)},
{name: '5-8:LOH', data: Array(5)},
{name: '9 :het', data: Array(5)},
{name: '9 :LOH', data: Array(5)}]
How can I sort the array so that the result will become:
sort =
[{name: '0:homdel', data: Array(3)},
{name: '1:LOH', data: Array(5)},
{name: '2:LOH', data: Array(5)},
{name: '3-4:LOH', data: Array(5)},
{name: '5-8:LOH', data: Array(5)},
{name: '9 :LOH', data: Array(5)},
{name: '2:het', data: Array(5)} ,
{name: '3-4:het', data: Array(5)},
{name: '5-8:het', data: Array(5)} ,
{name: '9 :het', data: Array(5)} ]
I have tried array.sort(customSort);
function customSort(a, b) {
let [countA, nameA] = a.name.split(':');
let [countB, nameB] = b.name.split(':');
let nameComparison = nameA.localeCompare(nameB);
let countComparison = parseInt(countA) - parseInt(countB);
if (nameComparison == 0) {
return countComparison;
} else {
return nameComparison;
}
}
but the result is not what I expected.
CodePudding user response:
Explanation in comments. Basically, remove the first element and sort the rest based on a custom tag.
let array = [
{name: '0:homdel', data: Array(3)},
{name: '1:LOH', data: Array(5)},
{name: '2:het', data: Array(5)},
{name: '2:LOH', data: Array(5)},
{name: '3-4:het', data: Array(5)},
{name: '3-4:LOH', data: Array(5)} ,
{name: '5-8:het', data: Array(5)},
{name: '5-8:LOH', data: Array(5)},
{name: '9 :het', data: Array(5)},
{name: '9 :LOH', data: Array(5)}]
const thesort = (arr) => {
// first grab the obj that is not getting sorted
let first = arr.shift();
// add the sortable tag
let narr = arr.map(a => ({ ...a,
tag: a.name.split(":")[1]
}))
.sort((a, b) => b.tag.localeCompare(a.tag)) // sort it
.map(a => { delete a.tag; return a }); // remove the custom tag
narr.unshift(first)
return narr
}
console.log(thesort(array))
CodePudding user response:
You could split the string and use some adjustments for the value.
const
array = [{ name: '0:homdel', data: [] }, { name: '1:LOH', data: [] }, { name: '2:het', data: [] }, { name: '2:LOH', data: [] }, { name: '3-4:het', data: [] }, { name: '3-4:LOH', data: [] }, { name: '5-8:het', data: [] }, { name: '5-8:LOH', data: [] }, { name: '9 :het', data: [] }, { name: '9 :LOH', data: [] }];
array.sort(({ name: a }, { name: b }) => {
const
split = s => {
let [value, text] = s.split(':');
if (value.includes(' ')) {
value = parseInt(value) .1;
} else if (value.includes('-')) {
let v = value.split('-').map(Number);
value = (v[0] v[1]) / 2;
}
return { value, text };
},
aa = split(a),
bb = split(b);
order = { homdel: 1, LOH: 2, het: 3 };
return order[aa.text] - order[bb.text] || aa.value - bb.value
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
Try this:
const array = [ {name: '0:homdel'}, {name: '1:LOH'}, {name: '2:het'}, {name: '2:LOH'}, {name: '3-4:het'}, {name: '3-4:LOH'} , {name: '5-8:het'}, {name: '5-8:LOH'}, {name: '9 :het'}, {name: '9 :LOH'} ];
const sorted = array.sort(({ name: a }, { name: b }) => {
if(b === '0:homdel') return 1;
else if(a === '0:homdel') return -1;
const [, nameA] = a.split(':'), [, nameB] = b.split(':');
return nameB.localeCompare(nameA);
});
console.log(sorted);