js按日期时间排序(正反序)
正反序区别与调用处理方法中的大小写
正序:
1 var dataContent = [ 2 { 3 ID: "1", 4 hobbit: "去音乐", 5 sport: "在篮球", 6 movies: "功夫", 7 sender: "elson", 8 received: "2017-08-18 13:23:21", 9 sex: "man", 10 age: "21" 11 }, 12 { 13 ID: "22", 14 hobbit: "我音乐", 15 sport: "怕篮球", 16 movies: "阿斯蒂", 17 sender: "neson", 18 received: "2017-03-18 19:23:21", 19 sex: "woman", 20 age: "22" 21 }, 22 { 23 ID: "32", 24 hobbit: "人音乐", 25 sport: "哦篮球", 26 movies: "发的", 27 sender: "nelson", 28 received: "2015-08-18 13:23:21", 29 sex: "man", 30 age: "22" 31 }, 32 { 33 ID: "14", 34 hobbit: "额音乐", 35 sport: "的篮球", 36 movies: "为个", 37 sender: "nlson", 38 received: "2017-01-18 13:23:21", 39 sex: "man", 40 age: "12" 41 }, 42 { 43 ID: "5", 44 hobbit: "他音乐", 45 sport: "人篮球", 46 movies: "问发", 47 sender: "nelon", 48 received: "2017-03-18 13:23:21", 49 sex: "woman", 50 age: "32" 51 }, 52 { 53 ID: "6", 54 hobbit: "有音乐", 55 sport: "打篮球", 56 movies: "雨天", 57 sender: "nson", 58 received: "2013-08-18 13:23:21", 59 sex: "man", 60 age: "22" 61 }, 62 { 63 ID: "7", 64 hobbit: "听音乐", 65 sport: "打篮球", 66 movies: "太过分哈", 67 sender: "nelsn", 68 received: "2017-08-18 10:56:21", 69 sex: "man", 70 age: "22" 71 }, 72 { 73 ID: "8", 74 hobbit: "听音乐", 75 sport: "打篮球", 76 movies: "功夫", 77 sender: "elon", 78 received: "2017-02-18 13:23:21", 79 sex: "man", 80 age: "22" 81 }, 82 ]; 83 84 //正序 85 dataContent = ForwardRankingDate(dataContent, "received"); 86 console.log(dataContent); 87 88 //封装的日期排序方法 89 function ForwardRankingDate(data, p) { 90 for (i = 0; i < data.length - 1; i++) { 91 for (j = 0; j < data.length - 1 - i; j++) { 92 console.log(Date.parse(data[j][p])); 93 if (Date.parse(data[j][p]) > Date.parse(data[j+1][p])) { 94 var temp = data[j]; 95 data[j] = data[j + 1]; 96 data[j + 1] = temp; 97 } 98 } 99 } 100 return data; 101 }
反序:
1 var dataContent = [ 2 { 3 ID: "1", 4 hobbit: "去音乐", 5 sport: "在篮球", 6 movies: "功夫", 7 sender: "elson", 8 received: "2017-08-18 13:23:21", 9 sex: "man", 10 age: "21" 11 }, 12 { 13 ID: "22", 14 hobbit: "我音乐", 15 sport: "怕篮球", 16 movies: "阿斯蒂", 17 sender: "neson", 18 received: "2017-03-18 19:23:21", 19 sex: "woman", 20 age: "22" 21 }, 22 { 23 ID: "32", 24 hobbit: "人音乐", 25 sport: "哦篮球", 26 movies: "发的", 27 sender: "nelson", 28 received: "2015-08-18 13:23:21", 29 sex: "man", 30 age: "22" 31 }, 32 { 33 ID: "14", 34 hobbit: "额音乐", 35 sport: "的篮球", 36 movies: "为个", 37 sender: "nlson", 38 received: "2017-01-18 13:23:21", 39 sex: "man", 40 age: "12" 41 }, 42 { 43 ID: "5", 44 hobbit: "他音乐", 45 sport: "人篮球", 46 movies: "问发", 47 sender: "nelon", 48 received: "2017-03-18 13:23:21", 49 sex: "woman", 50 age: "32" 51 }, 52 { 53 ID: "6", 54 hobbit: "有音乐", 55 sport: "打篮球", 56 movies: "雨天", 57 sender: "nson", 58 received: "2013-08-18 13:23:21", 59 sex: "man", 60 age: "22" 61 }, 62 { 63 ID: "7", 64 hobbit: "听音乐", 65 sport: "打篮球", 66 movies: "太过分哈", 67 sender: "nelsn", 68 received: "2017-08-18 10:56:21", 69 sex: "man", 70 age: "22" 71 }, 72 ]; 73 74 //反序 75 dataContent = ReverseRankingDate(dataContent, "received"); 76 console.log(dataContent); 77 78 //封装的日期排序方法 79 function ReverseRankingDate(data, p) { 80 for (i = 0; i < data.length - 1; i++) { 81 for (j = 0; j < data.length - 1 - i; j++) { 82 console.log(Date.parse(data[j][p])); 83 if (Date.parse(data[j][p]) < Date.parse(data[j+1][p])) { 84 var temp = data[j]; 85 data[j] = data[j + 1]; 86 data[j + 1] = temp; 87 } 88 } 89 } 90 return data; 91 }