Today to interview to do a set of questions, there is a problem is the array random arrangement. There is no concept in my mind, just because the last few days I looked at the fifth chapter of JavaScript Advanced Programming 3, the array ordering method reverse () and sort () are explained in the array. Reverse is to reverse the position of the elements in the array and return the inverted array, sorted by the Unicode code point of the string, and the JS sort is sorted by the sort method followed by the Unicode code point of the string (code Point), and only one of the parameters of the sort method is comparefunction, which translates to a comparison function.
Is the sort comparison rule.
Learn about the complete sort, regression topic. Array random order, see random, our first reaction is to use the Math.random function, sorting with sort, but how to sort? In the interview I have been thinking, and then after the interview I have Baidu a bit to find the answer.
Here is the complete code
var arr = ["1", "2", 1,2,6, "SDAs", "sqqwcm", "Qywcqkn", "David"]; function Compare () { return math.random () > 0.5? 1:-1;} var newArr = arr.sort (compare); Console.info (NEWARR);
Interview questions: Array random arrangement