-3

I have run into a bit of a problem when sorting an array of strings by the length of each string. So, lets suppose I have an array

arr = ["i","suck","at","programming","jk","sorta"]

After doing some research it looks like the proper way to do this is by doing this

 arr.sort(function sorting(a, b){
          return b.length - a.length; 
          })

I am very confused how this is processed. Can anyone walk me through how this accomplishes the task of sorting an array of strings by length? This function seems to be just return -1, 0,1 based on every two possible pairs of elements in the array? Please help.

theamateurdataanalyst
  • 2,794
  • 4
  • 38
  • 72
  • 1
    [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Waxen Sep 15 '14 at 22:38
  • 3
    A very very simplified answer is that the `-1`, `0` and `1` are helping the internal sorting algorithm figure out whether it needs to move an element left, leave it alone, or move it right (respectively). The algorithm stops when nothing else needs to be moved. You could research Quick sort or Merge sort for examples of sorting algorithm implementations. – Cᴏʀʏ Sep 15 '14 at 22:39
  • Visualize the process: http://www.sorting-algorithms.com/quick-sort – Cᴏʀʏ Sep 15 '14 at 22:43
  • The sort comparison function tells the sort algorithm how two elements from the list should be arranged. By calling it over and over again in the context of a great deal of cleverness, the sort algorithm can arrange the whole array into the order implied by the comparison function - in your case, by string length. – Pointy Sep 15 '14 at 22:45

3 Answers3

0

the sort method can take a function as argument
this function tells the sort method by its return value whether x<y, x>y or x==y (x and y being two variables sent by the sort method to the function)

  • if the return value was negative (not necessarily -1): then the first argument must come before the second

  • if the return value was 0:the two values are equal for the sorting

  • if the return value was positive (not necessarily 1): then the first argument must come after the second
  • PhpLou
    • 430
    • 3
    • 16
    0

    You have to remember, under the hood of the javascript engine, sort is simply a sorting algorithm. That algorithm can be any algorithm that accomplishes sorting. A browser could be using bubble sort (a terrible sorting algorithm generically speaking), or it could be using quick sort (more likely). In fact, if you look at similar questions, you'll find that browsers will use different sorting algorithms like quicksort or merge sort depending on the data type of the array to be sorted (e.g. Javascript Array.sort implementation?). At the heart of any sorting algorithm, from the most basic to the most complex, is a comparison between two items. In an iteration of the algorithm, the algorithm will decide, should item a be sorted lower or higher than item b (or the same)? That's it. The only real reason the algorithms get fairly complex is to optimize the efficiency of sorting. For example, on average, bubble sort will require on the order of n^2 operations to sort a list of n elements, whereas merge and quick sort will require n*log(n) (a much, much smaller number when n is large) to sort a list of n elements.

    But all that doesn't really matter for all you care, you just want the list to be sorted in whichever way you want. In your case, you are saying that you want an item of a smaller length to be sorted in a lower position in the resulting list as compared to another string whose length is larger. That is all that your comparison function does - it is a generic way for the algorithm to know how to order items. I know the documentation is out there on this stuff, but I wanted to give you my take.

    When you provide the comparison function to Array.sort, then internally the algorithm will know that on every iteration (which compares two individual items), the algorithm will call your function on those two given items, and based on your function's output for those two particular elements, it will sort accordingly.

    Community
    • 1
    • 1
    p e p
    • 6,593
    • 2
    • 23
    • 32
    -1

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

    If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

    If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.

    If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

    If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

    compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined

    Johann Echavarria
    • 9,695
    • 4
    • 26
    • 32