How to Implement digital sorting using the javascript sorting function-javascript tutorial

Source: Internet
Author: User
This article mainly introduces the information related to the implementation of digital sorting by the javascript sorting function. The example is attached. If you need it, refer to the following javascript sorting function to implement digital sorting.

Script function SortNumber (obj, func) // defines the universal sorting function {// parameter verification. if the first parameter is not an array or the second parameter is not a function, an exception is thrown if (! (Obj instanceof Array) |! (Func instanceof Function) {var e = new Error (); // generate Error message e. number = 100000; // defines the error code e. message = "invalid parameter"; // error description throw e; // throw an exception} for (n in obj) // start sorting {for (m in obj) {if (func (obj [n], obj [m]) // use the callback function for sorting. The rules are set by the user {var tmp = obj [n]; // create a temporary variable obj [n] = obj [m]; // exchange data obj [m] = tmp ;}} return obj; // return the sorted array} function greatThan (arg1, arg2) // callback function, user-defined sorting rule {return arg1 <arg2 ;} try {var numAry = new Array (, 9); // generate an Array of documents. write ("
  • Before sorting: "+ numAry); // output the array SortNumber (numAry, greatThan) Before sorting; // call the sorting function document. write ("
  • After sorting: "+ numAry); // output the sorted array} catch (e) {alert (e. number +": "+ e. message);} script
  • The above is all the content of this article. I hope you will like it.

    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.