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.