欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Javascript 數(shù)組排序詳解

 更新時(shí)間:2014年10月22日 10:35:13   投稿:hebedich  
JavaScript實(shí)現(xiàn)多維數(shù)組、對(duì)象數(shù)組排序,其實(shí)用的就是原生的sort()方法,用于對(duì)數(shù)組的元素進(jìn)行排序。今天我們就來詳細(xì)探討下sort()方法

如果你接觸javascript有一段時(shí)間了,你肯定知道數(shù)組排序函數(shù)sort,sort是array原型中的一個(gè)方法,即array.prototype.sort(),sort(compareFunction),其中compareFunction是一個(gè)比較函數(shù),下面我們看看來自Mozilla MDN 的一段描述:
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic (“dictionary” or “telephone book,” not numerical) order. For example, “80″ comes before “9″ in lexicographic order, but in a numeric sort 9 comes before 80.

下面看些簡單的例子:

復(fù)制代碼 代碼如下:

// Output [1, 2, 3]
console.log([3, 2, 1].sort());

// Output ["a", "b", "c"]
console.log(["c", "b", "a"].sort());

// Output [1, 2, "a", "b"]
console.log(["b", 2, "a", 1].sort());


從上例可以看出,默認(rèn)是按字典中字母的順序來排序的。

幸運(yùn)的是,sort接受一個(gè)自定義的比較函數(shù),如下例:

復(fù)制代碼 代碼如下:

function compareFunction(a, b) {
 if( a > b) {
  return -1;
 }else if(a < b) {
  return 1;
 }else {
  return 0;
 }
}
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction));

排序完我們又有個(gè)疑問,如何控制升序和降序呢?

復(fù)制代碼 代碼如下:

function compareFunction(flag) {
 flag = flag ? flag : "asc";
 return function(a, b) {
  if( a > b) {
   return flag === "desc" ? -1 : 1;
  }else if(a < b) {
   return flag === "desc" ? 1 : -1;
  }else {
   return 0;
  }
 };
}
//Outputs ["1", "Benjamin", "zuojj"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction()));
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction("desc")));

comparFunction的排序規(guī)則是這樣的:
1.If it returns a negative number, a will be sorted to a lower index in the array.
2.If it returns a positive number, a will be sorted to a higher index.
3.And if it returns 0 no sorting is necessary.

下面我們來看看摘自Mozilla MDN上的一段話:
The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2.為了解釋這段描述,我們來看個(gè)例子:

In JavaScript 1.1, on some platforms, the sort method does not work. This method works on all platforms for JavaScript 1.2.

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array.詳情請(qǐng)戳這里。

復(fù)制代碼 代碼如下:

var arr = [];
arr[0] = "Ant";
arr[5] = "Zebra";
//Outputs ["Ant", 5: "Zebra"]
console.log(arr);
//Outputs 6
console.log(arr.length);
//Outputs "Ant*****Zebra"
console.log(arr.join("*"));
//排序
var sortArr = arr.sort();
//Outputs ["Ant", "Zebra"]
console.log(sortArr);
//Outputs 6
console.log(sortArr.length);
//Outputs "Ant*Zebra****"
console.log(sortArr.join("*"));

希望本文對(duì)你學(xué)習(xí)和了解sort()方法有幫助,文中不妥之處還望批評(píng)斧正。

參考鏈接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

相關(guān)文章

  • JavaScript的字符串方法匯總

    JavaScript的字符串方法匯總

    字符串的操作在js中非常頻繁,也非常重要。以往看完書之后都能記得非常清楚,但稍微隔一段時(shí)間不用,便會(huì)忘得差不多,記性不好是硬傷啊。。。今天就對(duì)字符串的一些常用操作做個(gè)整理,一者加深印象,二者方便今后溫習(xí)查閱。
    2016-07-07
  • ajax與jsonp的區(qū)別及用法

    ajax與jsonp的區(qū)別及用法

    在本篇文章里我們給大家分享了ajax與jsonp的區(qū)別的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2018-10-10
  • javascript日期格式化示例分享

    javascript日期格式化示例分享

    這篇文章主要介紹了javascript日期格式化示例,需要的朋友可以參考下
    2014-03-03
  • JavaScript基本概念初級(jí)講解論壇貼的學(xué)習(xí)記錄

    JavaScript基本概念初級(jí)講解論壇貼的學(xué)習(xí)記錄

    JavaScript基本概念 論壇貼建議大家看下,都是一些js的高級(jí)的技巧知識(shí)小結(jié)。
    2009-02-02
  • document.getElementById獲取控件對(duì)象為空的解決方法

    document.getElementById獲取控件對(duì)象為空的解決方法

    今天寫個(gè)網(wǎng)頁,想在頁面加載onLoad時(shí),動(dòng)態(tài)顯示由后臺(tái)其他程序傳來的數(shù)據(jù)時(shí),用document.getElementById獲取控件對(duì)象總是為空。但是檢查了這個(gè)id確實(shí)是存在的??聪挛牡氖纠徒鉀Q方法
    2013-11-11
  • Javascript中Date類型和Math類型詳解

    Javascript中Date類型和Math類型詳解

    這篇文章主要給大家介紹了Javascript中Date類型和Math類型的一些基礎(chǔ)知識(shí),非常的實(shí)用,有需要的小伙伴可以參考下
    2016-02-02
  • 詳解js加減乘除精確計(jì)算

    詳解js加減乘除精確計(jì)算

    這篇文章主要介紹了js加減乘除精確計(jì)算,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • JavaScript CSS修改學(xué)習(xí)第一章 查找位置

    JavaScript CSS修改學(xué)習(xí)第一章 查找位置

    在這一章我會(huì)給出查找頁面上的元素位置的代碼。他能幫助你找到真正的位置,假如你改變了窗口的尺寸,然后再次運(yùn)行代碼,也會(huì)告訴你新的位置。
    2010-02-02
  • 淺談Web頁面向后臺(tái)提交數(shù)據(jù)的方式和選擇

    淺談Web頁面向后臺(tái)提交數(shù)據(jù)的方式和選擇

    下面小編就為大家?guī)硪黄獪\談Web頁面向后臺(tái)提交數(shù)據(jù)的方式和選擇。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09

最新評(píng)論