1 //日期排序 2 function sortDownDate(a, b) { 3 return Date.parse(a.received) - Date.parse(b.received); 4 } 5 6 function sortUpDate(a, b) { 7 return Date.parse(b.received) - Date.parse(a.received); 8 } 9 10 11 var arr=[ 12 { 13 sender: "elson", 14 received: "2017-08-18 13:23:21", 15 age: "21" 16 }, 17 { 18 sender: "neson", 19 received: "2017-03-18 19:23:21", 20 age: "22" 21 }, 22 { 23 sender: "nelson", 24 received: "2015-08-18 13:23:21", 25 age: "22" 26 }, 27 { 28 sender: "lson", 29 received: "2018-01-18 12:12:12", 30 age: "56" 31 }, 32 { 33 sender: "nelson", 34 received: "2011-02-18 13:23:21", 35 age: "22" 36 }, 37 { 38 sender: "son", 39 received: "2011-08-18 3:3:3", 40 age: "22" 41 }, 42 { 43 sender: "eson", 44 received: "2017-01-18 13:23:21", 45 age: "22" 46 }, 47 { 48 sender: "nelson", 49 received: "2017-08-18 6:6:6", 50 age: "34" 51 }, 52 { 53 sender: "elon", 54 received: "2017-02-18 13:23:21", 55 age: "22" 56 }, 57 { 58 sender: "elon", 59 received: "2017-04-18 13:23:21", 60 age: "22" 61 }, 62 { 63 sender: "lson", 64 received: "2017-06-18 13:23:21", 65 age: "22" 66 }, 67 { 68 sender: "eon", 69 received: "2017-08-18 13:23:21", 70 age: "22" 71 }, 72 ]; 73 74 //console.log(arr.sort(sortDownDate));//正序 75 //console.log(arr.sort(sortUpDate));//反序
结果如下:
左为正序
(arr.sort(sortDownDate)),右为反序(arr.sort(sortUpDate))
这是其他排序的方法,欢迎查看,欢迎给意见!
按JavaScript非数字(中文)排序
按JavaScript按纯数字排序