-3

I am a beginner in Javascript , and now i am reading a book , where i am confused with one of the tasks solution. var arr = [1, 2, 3, 4, 5] :

With the help of sort function we have to get an random order in an array. Here is the authors solution `

function compareRandom(a, b) {
return Math.random() - 0.5 ;
}
var arr = [1,2,3,4];
arr.sort(compareRandom);
alert( arr ); // elements are in a random order ` [3,5,1,2,4]

So i cant understand how the function works , its realization step by step. IS there someone who can explain it to me???

Vincent1989
  • 1,593
  • 2
  • 13
  • 25
Norayr Ghukasyan
  • 93
  • 1
  • 1
  • 6
  • 3
    You have not searched. Why? – Tomalak Oct 21 '17 at 13:20
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Madhawa Priyashantha Oct 21 '17 at 13:21
  • 1
    Oh come on. He at least asked a good question. – Jonas Wilms Oct 21 '17 at 13:22
  • 2
    You want to read the documentation of Array#sort and the documentation of Math#random. And then you want to search for that question and read 10 or 15 answers. – Tomalak Oct 21 '17 at 13:23
  • 1
    @Jonasw No, he asked *"please write an explanation because I can't be bothered to at least read the basic documentation"*. That's all people do these days, and I'm getting sick of it. – Tomalak Oct 21 '17 at 13:24
  • I can't think of why people downvoting by not consider him a beginner. He has read the documentation and tried with a sample code but just asking help to understand it. Please try to help him without simply downvoting. – Vijayanath Viswanathan Oct 21 '17 at 13:25
  • @VijayanathViswanathan the point is that some basic research into both would allow OP to ask a more informed question than *"I don't understand any of it"*. This isn't a tutorial service – charlietfl Oct 21 '17 at 13:28
  • or [What's the logic behind the SomeArray.sort ( function() { … } ) statement?](https://stackoverflow.com/questions/12449479/whats-the-logic-behind-the-somearray-sort-function-statement) – t.niese Oct 21 '17 at 13:28
  • 3
    @Vijayanath There is nothing wrong with being a beginner. But *"I can't understand this"* simply is not a good question. This particular question has been answered here and elsewhere so many times that it's not even funny anymore. Arriving here and saying "Hey I'm a beginner, explain it to me all over again" is rude, it's as simple as that. – Tomalak Oct 21 '17 at 13:28
  • It's dismissive of the time others have already invested into answering beginner questions, it's lazy, and maybe worst of all, it shows a severe lack of curiosity. Why bother explaining stuff to people who are both lazy and not curious? – Tomalak Oct 21 '17 at 13:33
  • 1
    it is a very bad approach to randomize arrays, because it does not distribute the items equally. – Nina Scholz Oct 21 '17 at 13:36
  • Hey people ... I cant understand you ... Stackoverflow is the website to help each other , but instead of doing it , all youu just undervoting ... I am not stupid , and i have searched many times in google , aksed many programmers in real life , but nobody could help me and i decided to ask it here ... – Norayr Ghukasyan Oct 22 '17 at 08:31

1 Answers1

1

The one you are passing as a parameter to sort function is called compareFunction, which is optional. compareFunction is a function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like:

function(a, b){return a-b}

When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

Example:

When comparing 40 and 100, the sort() method calls the compare function(40,100).

The function calculates 40-100, and returns -60 (a negative value). The sort function will sort 40 as a value lower than 100.

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43