Modified the sort function (#25978)
Modification in the sort function to implement the better practice of returning 0 for the same elements. Not implementing the practice can break the function in new browsers.
This commit is contained in:
		
				
					committed by
					
						 Aditya
						Aditya
					
				
			
			
				
	
			
			
			
						parent
						
							e3059ee4c8
						
					
				
				
					commit
					02206a2123
				
			| @@ -8,7 +8,7 @@ challengeType: 1 | ||||
| <section id='description'> | ||||
| The <code>sort</code> method sorts the elements of an array according to the callback function. | ||||
| For example: | ||||
| <blockquote>function ascendingOrder(arr) {<br>  return arr.sort(function(a, b) {<br>    return a - b;<br>  });<br>}<br>ascendingOrder([1, 5, 2, 3, 4]);<br>// Returns [1, 2, 3, 4, 5]<br><br>function reverseAlpha(arr) {<br>  return arr.sort(function(a, b) {<br>    return a < b;<br>  });<br>}<br>reverseAlpha(['l', 'h', 'z', 'b', 's']);<br>// Returns ['z', 's', 'l', 'h', 'b']</blockquote> | ||||
| <blockquote>function ascendingOrder(arr) {<br>  return arr.sort(function(a, b) {<br>    return a - b;<br>  });<br>}<br>ascendingOrder([1, 5, 2, 3, 4]);<br>// Returns [1, 2, 3, 4, 5]<br><br>function reverseAlpha(arr) {<br>  return arr.sort(function(a, b) {<br>    return a === b ? 0 : a < b ? : 1 : -1;<br>  });<br>}<br>reverseAlpha(['l', 'h', 'z', 'b', 's']);<br>// Returns ['z', 's', 'l', 'h', 'b']</blockquote> | ||||
| Note: It's encouraged to provide a callback function to specify how to sort the array items. JavaScript's default sorting method is by string Unicode point value, which may return unexpected results. | ||||
| </section> | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user