score:3

Accepted answer
const arr = [
  { chromosome: '2', startPosition: '980000', endPosition: '989000' },
  { chromosome: '2', startPosition: '978000', endPosition: '979000' },
  { chromosome: '1', startPosition: '978000', endPosition: '979000' },
  { chromosome: '10', startPosition: '978000', endPosition: '979000' },
  { chromosome: 'x', startPosition: '978000', endPosition: '979000' },
  { chromosome: 'x', startPosition: '5', endPosition: '979000' },
  { chromosome: '1', startPosition: '978000', endPosition: '9999999' },
];

const getVal = (obj) => {
  const chr = obj.chromosome;
  const chromVal = chr === 'y'
    ? 25
    : chr === 'x'
      ? 24
      : Number(chr);
  return (
    chromVal * 1e20 +
    obj.startPosition * 1e10 +
    Number(obj.endPosition)
  );
  return totalVal;
};

arr.sort((a, b) => getVal(a) - getVal(b));
console.log(arr);

score:2

const item = [{
    chromosome: '2',
    startPosition: '980000',
    endPosition: '989000'
}, {
    chromosome: '2',
    startPosition: '40',
    endPosition: '60'
}, {
    chromosome: '1',
    startPosition: '978000',
    endPosition: '979000'
}, {
    chromosome: '10',
    startPosition: '978000',
    endPosition: '979000'
}, {
    chromosome: 'x',
    startPosition: '978000',
    endPosition: '979000'
}];

const LETTERS = { x: '23', y: '24', m: '25' }
const CHROMOSOME_DIGITS = 2
const POSITION_DIGITS = 10

const toStr = ({chromosome, startPosition, endPosition}) => 
    ((LETTERS[chromosome] || chromosome.padStart(CHROMOSOME_DIGITS,'0')) + 
    startPosition.padStart(POSITION_DIGITS,'0') + 
    endPosition.padStart(POSITION_DIGITS,'0'))

console.log(item.sort((a,b) => toStr(a).localeCompare(toStr(b))))

Related Query

More Query from same tag