數組數據的排序sort


var carList = [{ value: "魯", text: "L-魯" }, { value: "澳", text: "A-澳" }, { value: "B", text: "B" }, { value: "C", text: "C" }, { value: "川", text: "C-川" }, { value: "鄂", text: "E-鄂" }, { value: "G", text: "G" }, { value: "贛", text: "G-贛" }, { value: "桂", text: "G-桂" }, { value: "貴", text: "G-貴" }, { value: "甘", text: "G-甘" }, { value: "港", text: "G-港" }, { value: "H", text: "H" }, { value: "黑", text: "H-黑" }, { value: "滬", text: "H-滬" }, { value: "J", text: "J" }, { value: "京", text: "J-京" }, { value: "津", text: "J-津" }, { value: "冀", text: "J-冀" }, { value: "晉", text: "J-晉" }, { value: "吉", text: "J-吉" }, { value: "K", text: "K" }, { value: "L", text: "L" }, { value: "遼", text: "L-遼" }, { value: "蒙", text: "M-蒙" }, { value: "閩", text: "M-閩" }, { value: "N", text: "N" }, { value: "寧", text: "N-寧" }, { value: "青", text: "Q-青" }, { value: "瓊", text: "Q-瓊" }, { value: "S", text: "S" }, { value: "蘇", text: "S-蘇" }, { value: "陝", text: "S-陝" },{ value: "臺", text: "T-臺" }, { value: "V", text: "V" }, { value: "WJ", text: "WJ" }, { value: "皖", text: "W-皖" }, { value: "湘", text: "X-湘" }, { value: "新", text: "X-新" }, { value: "粵", text: "Y-粵" }, { value: "雲", text: "Y-雲" }, { value: "豫", text: "Y-豫" }, { value: "渝", text: "Y-渝" }, { value: "Z", text: "Z" }, { value: "浙", text: "Z-浙" }, { value: "藏", text: "Z-藏" }]

  

var newcity = [];
		for(var i = 0;i< carList.length;i++){
			console.log(carList[i].text);
			newcity.push(carList[i].text);
		}

		console.log(newcity);

		document.write(newcity + "<br />")
		document.write(newcity.sort()+ '<br/>');

  

var newcarlist = [];
		var cityname = [];
		for(var i=0;i<newcity.length;i++){
			// console.log(newcity[i]);
			var value = newcity[i].split("-");
			// console.log(value[0])
			var json = {
				value: value[0],
				text: newcity[i]
			}
			if(newcity[i] == 'L-魯'){
				newcarlist.unshift(json);
			}else{
				newcarlist.push(json);
			}
			
			if(value[1] == null){
				cityname.push(value[0])
			}else {
				cityname.push(value[1]);
			}
			
		}
		console.log(newcarlist)
		console.log(JSON.stringify(newcarlist))
		document.write(JSON.stringify(newcarlist));

  

補充:json

	function sortNumber(a,b)
		{
		return a-b
		}

		var arr = new Array(6)
		arr[0] = "10"
		arr[1] = "5"
		arr[2] = "40"
		arr[3] = "25"
		arr[4] = "1000"
		arr[5] = "1"

		 document.write(arr + "<br />")
		 document.write(arr.sort(sortNumber))

  

	function sortNumber(a,b)
		{
		return b-a
		}

		var arr = new Array(6)
		arr[0] = "10"
		arr[1] = "5"
		arr[2] = "40"
		arr[3] = "25"
		arr[4] = "1000"
		arr[5] = "1"

		 document.write(arr + "<br />")
		 document.write(arr.sort(sortNumber))

  

sort() 方法用於對數組的元素進行排序。
arrayObject.sort(sortby)
sortby	可選。規定排序順序。必須是函數。
返回值
對數組的引用。請注意,數組在原數組上進行排序,不生成副本。
說明
若是調用該方法時沒有使用參數,將按字母順序對數組中的元素進行排序,說得更精確點,是按照字符編碼的順序進行排序。要實現這一點,首先應把數組的元素都轉換成字符串(若有必要),以便進行比較。
若是想按照其餘標準進行排序,就須要提供比較函數,該函數要比較兩個值,而後返回一個用於說明這兩個值的相對順序的數字。比較函數應該具備兩個參數 a 和 b,其返回值以下:
若 a 小於 b,在排序後的數組中 a 應該出如今 b 以前,則返回一個小於 0 的值。
若 a 等於 b,則返回 0。
若 a 大於 b,則返回一個大於 0 的值。
相關文章
相關標籤/搜索