0

I'm trying so hard to sort these arrays numerically and alphabetically, except that it only sorts them based on their length only (especially numbers). Please help...

Tips: Open in IDE and click on the arrow or on the top of the table to sort them, then click again if arrow is pointing down. Cheers...

2 Answers2

0

Have you considered doing a nested sort. for example doing something like:

 myArray = myArray.sort((a,b) => a[column] < b[column] ? 1 : -1).sort((a,b)=>a[newcolumn] - b [newvolumn])
0

It's quite unclear from your post, what the expected sorting logic is supposed to be. Simply saying "sort these arrays numerically and alphabetically" is not very specific.

What should be sorted numerically and what should be sorted alphabetically? Is there a sorting order? Are you looking to sort something alphabetically first and then something numerically second?

Regardless of the answer, I imagine you are facing the old JavaScript integers sorting issue:

How to sort an array of integers correctly

JavaScript, out of the box, will sort integers as Strings, not as integers.

  • Sorting the strings shown in the code fam, the adresses, emails, names... – Elias Fitzgerald Jul 28 '22 at 21:44
  • When asking questions here... the community needs more specificity in what you are looking for. When I originally loaded your code into a `.html` file, I saw a table with the columns you mentioned. Your post hasn't given clear requirements. For example, when clicking on the emails column that has the same e-mail address for multiple rows, how should the row be sorted? Should we then refer to a different column to sort the ones with same email addresses? What's the expectation here? Sorting Strings is easy: I think you are looking for more complexity than that. – Eric Lingamfelter Aug 01 '22 at 02:50