callback function, user-defined collation

Source: Internet
Author: User
Tags throw exception

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml" ><Head>    <title>Example 6-3</title></Head><Body><Scriptlanguage= "JavaScript">    functionsortnumber (obj, func)//defining a universal sort function    {        //parameter validation, throws an exception if the first parameter is not an array or the second argument is not a function        if( !(objinstanceofArray)|| !(funcinstanceofFunction)) {            vare= NewError (); //Generate error messageE.number= 100000; //define the error numberE.message= "Invalid parameter"; //Error Description            Throwe; //Throw Exception        }         for(ninchobj)//Start Sorting        {             for(Minchobj) {                if(func (Obj[n], obj[m]))//use callback functions to sort the rules set by the user                {                    vartmp=Obj[n]; Obj[n]=Obj[m]; OBJ[M]=tmp; }            }        }        returnobj; //returns the sorted array    }    functionGreatthan (arg1, arg2)//callback function, user-defined collation    {        returnarg1>arg2; //rules: From big to small    }    Try    {        varnumary= NewArray (5,8,6, +,1, $,7, - ); //generate an arraydocument.write ("<li> before sorting:"+numary); //data before the output is sortedSortnumber (numary, Greatthan)//Call sort functiondocument.write ("<li> after sorting:"+numary); //output sorted Array    }    Catch(e) {alert (E.number+":"+e.message); //Exception Handling    }</Script></Body></HTML>

callback function, user-defined collation

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.