-5

How can we Sort numbers in words (one, two, three) in Javascript (Angularjs)


My array has some numeric words

$scope.Numbers = ["three", "one", "five", "two", ...... "hundred", "four"];

I want the result to be:

one
two
three
four
...
...
...
hundred

I have searched Google for the solution, but I have not found anything

Also i have tried array.sort(), but it is sorting alphabetically.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • If you want to sort it by **meaning**, you'll need a lookup array. – Ravan Scafi Apr 13 '15 at 12:47
  • 2
    As neither angular nor javascript know english, how could they know that 'three' is lower than 'four' ? – xlecoustillier Apr 13 '15 at 12:49
  • I think you'll need to have an object or array that maps these words to the appropriate numbers, and compare the numbers instead of the words. – forgivenson Apr 13 '15 at 12:49
  • @X.L.Ant , If angular know the knowledge about numbers, then why angular don't have knowledge about English ? – Ramesh Rajendran Apr 13 '15 at 12:51
  • Why down vote? can you explain to me? – Ramesh Rajendran Apr 13 '15 at 12:53
  • @RameshRajendran Because then I would complain that it doesn't know maggyar as well. Or esperanto. – xlecoustillier Apr 13 '15 at 12:53
  • 4
    Because your question is phrased in a way that does not indicate a lot of research performed on the subject before asking. It sounds like you're asking for code. It's also why instead of providing you code in my answer I've provided a methodology - so you can be sure you actually understand the issue and solution (and why I took that approach) yourself and so can future visitors. – Benjamin Gruenbaum Apr 13 '15 at 12:55

2 Answers2

8

Here is a simple multi-step solution:

We effectively reduced the problem of sorting words to the problem of sorting numbers and the problem of converting numbers to and from words.

The greater picture is that by reusing the solutions of 3 smaller problems we solved a bigger issue. This is a fundamental part of programming anywhere and here in particular - by finding smaller subproblems and applying them to the task at hand we've solved it quickly and without ever having to actually implement a sorting function on words themselves.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
1

Going to elaborate a bit on what people have written in the comments for you. Simply put, the compiler can't know which word is higher or lower than any other, since words are just a collection of chars. It's pretty much like saying: Which word has a higher value, "chicken" or "car"? Or how can the compiler know that you are even writing in English? What happens if you write the numbers in words but in other languages? Basically, to the compiler each string is a set of chars and it doesn't care which letters or words they are since it doesn't matter from its point of view. If you compare it to a number instead, a number is a legit structure which has properties and mathematical rules to follow. I believe this is the reason people are downvoting your question.

There is no basis for a sorting algorithm to start. Thus, you need to convert the strings to integers first, then sort them, and then convert them back to strings if you want to have them ordered - just like Benjamin above explained.

ClockWise
  • 1,509
  • 15
  • 32