<p>本文实例总结了js索引数组的排序技巧,分享给大家供大家参考,具体如下:</p><p>定义数据如下:</p><pre class="brush:js;toolbar:false">var array=[12,22,6,31,24,78,57];</pre><p style="white-space: normal;">直接使用排序的方法:</p><pre class="brush:js;toolbar:false">// 升序 console.log(array.sort(function (x,y) {return x-y;})); // 降序 console.log(array.sort(function (x,y) {return y-x;}));</pre><p style="white-space: normal;">或者使用下面这种封装的方法,代码如下:</p><p style="white-space: normal;">定义排序函数:</p><pre class="brush:js;toolbar:false">/** * 索引数组排序 * @param {int} order 升序或降序(1表示升序,2表示降序) * @return {array} */ function sequence ( order ) { return function (x,y) { return order?x-y:y-x; } }</pre><p style="white-space: normal;">使用方法:</p><pre class="brush:js;toolbar:false">// 升序排序 console.log(array.sort(sequence(1))); // 降序排序 console.log(array.sort(sequence(0)));</pre><p style="white-space: normal;"><span style="color: rgb(255, 0, 0);">注意:方法中的1表示升序,0表示降序,这里可以根据自己的数据指定排序字段;</span></p><p>以上就是关于“js或jquery中索引数组升序(从小到大)或降序(从大到小)排序”的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流。</p>