Interview questions: Array random arrangement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.