3

The current output of my array is:

"10 FOOT", "45 RPM", "9 VOLT", "910D0", "AGUA", "BEAST", "KEN 5", "NEMCO"

What I want is:

"AGUA", "BEAST", "KEN 5", "NEMCO", "9 VOLT", "10 FOOT", "45 RPM:, "910D0"

How do I achieve this?

Theo ford
  • 63
  • 6
  • why would a number at the end have precedence over sorting alpha numerically? No wait, what has actually that number at the end have anything to do with the question? – Roko C. Buljan Apr 04 '21 at 12:24
  • does this post solve your question? https://stackoverflow.com/questions/18939726/javascript-array-sorting-only-first-letter-only-first-number – MWO Apr 04 '21 at 12:25
  • You need to explain more about your sorting criteria... – Majed Badawi Apr 04 '21 at 12:28
  • 1
    Let's clarify a bit, is this what you want? If an element starts with a letter, sort it alphabetically. If it starts with a number, sort it by the value of the number (not only the first digit), and finally, those starting with a number should go after the ones starting with a letter? – blex Apr 04 '21 at 12:30
  • Please post compilable code. Those quotes throw an error. – adiga Apr 04 '21 at 12:31
  • sorry if I was unclear. I'll try and demonstrate with examples. 1) http://tf-ref.com/wp_martinez_1/ - this works perfectly in wordpress/php you can see the desired order here (scroll to the bottom to see how numbers are correctly ordered, and following on from letters). 2) in javascript/react however I am getting - https://martinez-gallery-build.netlify.app/ by default. – Theo ford Apr 04 '21 at 12:31
  • the above comment by 'blex' states in better words than I can muster, what I am effectively trying to achieve – Theo ford Apr 04 '21 at 12:34

4 Answers4

2

You could check if the string starts with a digit and sort the rest by groups.

const array = ['10 FOOT', '45 RPM', '9 VOLT', '910D0', 'AGUA', 'BEAST', 'KEN 5', 'NEMCO'];

array.sort((a, b) => isFinite(a[0]) - isFinite(b[0])
    || a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })
);

console.log(array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

const order = ["10 FOOT", "45 RPM", "9 VOLT", "910D0", "AGUA", "BEAST", "KEN 5", "NEMCO"];

order.sort((a, b) => /^[0-9]/.test(a) - /^[0-9]/.test(b) || a.localeCompare(b, undefined, { numeric: true }));

console.log(order);

Sort elements starting with a number first, afterwards use localeCompare's numeric option (which ensures "10" > "2").

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

const arr = ["10 FOOT", "45 RPM", "9 VOLT", "910D0", "AGUA", "BEAST", "KEN 5", "NEMCO"];

const customCompare = (a, b) => 
  /^\d/.test(a) - /^\d/.test(b) || 
  a.localeCompare(b, undefined, { numeric: true });

const sorted = arr.sort(customCompare);

console.log(...sorted);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

You could create 2 new arrays, sort them and put them back together:

const arr = ["10 FOOT", "45 RPM", "9 VOLT", "910D0", "AGUA", "BEAST", "KEN 5", "NEMCO"];
const nums = arr.filter(a => parseInt(a))
const words = arr.filter(a => !parseInt(a))

nums.sort((a,b) => parseInt(a) -parseInt(b))
words.sort()

const finalArray = words.concat(nums)
PixAff
  • 309
  • 4
  • 14