JavaScript按日期排序之灵活排序

 

  上代码:

  

  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: "lson",
 78                 received: "2018-08-18 13:23:21",
 79                 sex: "woman",
 80                 age: "56"
 81             },
 82             {
 83                 ID: "9",
 84                 hobbit: "听音乐",
 85                 sport: "打篮球",
 86                 movies: "功夫",
 87                 sender: "nelson",
 88                 received: "2011-08-18 13:23:21",
 89                 sex: "man",
 90                 age: "22"
 91             },
 92             {
 93                 ID: "10",
 94                 hobbit: "听音乐",
 95                 sport: "打篮球",
 96                 movies: "功夫",
 97                 sender: "son",
 98                 received: "2011-08-18 13:23:21",
 99                 sex: "man",
100                 age: "22"
101             },
102             {
103                 ID: "11",
104                 hobbit: "听音乐",
105                 sport: "打篮球",
106                 movies: "功夫",
107                 sender: "eson",
108                 received: "2017-01-18 13:23:21",
109                 sex: "woman",
110                 age: "22"
111             },
112             {
113                 ID: "12",
114                 hobbit: "听音乐",
115                 sport: "打篮球",
116                 movies: "功夫",
117                 sender: "nelson",
118                 received: "2017-08-18 13:23:21",
119                 sex: "man",
120                 age: "34"
121             },
122             {
123                 ID: "13",
124                 hobbit: "听音乐",
125                 sport: "打篮球",
126                 movies: "功夫",
127                 sender: "elon",
128                 received: "2017-02-18 13:23:21",
129                 sex: "man",
130                 age: "22"
131             },
132             {
133                 ID: "14",
134                 hobbit: "听音乐",
135                 sport: "打篮球",
136                 movies: "功夫",
137                 sender: "nlon",
138                 received: "2017-08-18 13:23:21",
139                 sex: "woman",
140                 age: "12"
141             },
142             {
143                 ID: "15",
144                 hobbit: "听音乐",
145                 sport: "打篮球",
146                 movies: "功夫",
147                 sender: "elon",
148                 received: "2017-04-18 13:23:21",
149                 sex: "man",
150                 age: "22"
151             },
152             {
153                 ID: "16",
154                 hobbit: "听音乐",
155                 sport: "打篮球",
156                 movies: "功夫",
157                 sender: "elon",
158                 received: "2017-08-18 13:23:21",
159                 sex: "woman",
160                 age: "64"
161             },
162             {
163                 ID: "17",
164                 hobbit: "听音乐",
165                 sport: "打篮球",
166                 movies: "功夫",
167                 sender: "nelson",
168                 received: "2017-06-18 13:12:21",
169                 sex: "man",
170                 age: "22"
171             },
172             {
173                 ID: "18",
174                 hobbit: "听音乐",
175                 sport: "打篮球",
176                 movies: "功夫",
177                 sender: "lson",
178                 received: "2017-06-18 13:23:21",
179                 sex: "man",
180                 age: "22"
181             },
182             {
183                 ID: "19",
184                 hobbit: "听音乐",
185                 sport: "打篮球",
186                 movies: "功夫",
187                 sender: "n",
188                 received: "2017-08-18 14:23:21",
189                 sex: "woman",
190                 age: "43"
191             },
192             {
193                 ID: "20",
194                 hobbit: "听音乐",
195                 sport: "打篮球",
196                 movies: "功夫",
197                 sender: "on",
198                 received: "2017-08-18 13:13:21",
199                 sex: "man",
200                 age: "22"
201             },
202             {
203                 ID: "21",
204                 hobbit: "听音乐",
205                 sport: "打篮球",
206                 movies: "功夫",
207                 sender: "non",
208                 received: "2017-08-18 16:23:21",
209                 sex: "woman",
210                 age: "15"
211             },
212             {
213                 ID: "22",
214                 hobbit: "听音乐",
215                 sport: "打篮球",
216                 movies: "功夫",
217                 sender: "eon",
218                 received: "2017-08-18 13:23:21",
219                 sex: "man",
220                 age: "22"
221             },
222         ];
223 
224     //正序
225     dataContent = ForwardRankingDate(dataContent, "received");
226     console.log(dataContent);
227 
228     //封装的日期排序方法
229     function ForwardRankingDate(data, p) {
230     for (i = 0; i < data.length - 1; i++) {
231         for (j = 0; j < data.length - 1 - i; j++) {
232             console.log(Date.parse(data[j][p]));
233             if (Date.parse(data[j][p]) > Date.parse(data[j+1][p])) {
234                 var temp = data[j];
235                 data[j] = data[j + 1];
236                 data[j + 1] = temp;
237             }
238         }
239     }
240     return data;
241 }

  反序:

  

  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: "lson",
 78                 received: "2018-08-18 13:23:21",
 79                 sex: "woman",
 80                 age: "56"
 81             },
 82             {
 83                 ID: "9",
 84                 hobbit: "听音乐",
 85                 sport: "打篮球",
 86                 movies: "功夫",
 87                 sender: "nelson",
 88                 received: "2011-08-18 13:23:21",
 89                 sex: "man",
 90                 age: "22"
 91             },
 92             {
 93                 ID: "10",
 94                 hobbit: "听音乐",
 95                 sport: "打篮球",
 96                 movies: "功夫",
 97                 sender: "son",
 98                 received: "2011-08-18 13:23:21",
 99                 sex: "man",
100                 age: "22"
101             },
102             {
103                 ID: "11",
104                 hobbit: "听音乐",
105                 sport: "打篮球",
106                 movies: "功夫",
107                 sender: "eson",
108                 received: "2017-01-18 13:23:21",
109                 sex: "woman",
110                 age: "22"
111             },
112             {
113                 ID: "12",
114                 hobbit: "听音乐",
115                 sport: "打篮球",
116                 movies: "功夫",
117                 sender: "nelson",
118                 received: "2017-08-18 13:23:21",
119                 sex: "man",
120                 age: "34"
121             },
122             {
123                 ID: "13",
124                 hobbit: "听音乐",
125                 sport: "打篮球",
126                 movies: "功夫",
127                 sender: "elon",
128                 received: "2017-02-18 13:23:21",
129                 sex: "man",
130                 age: "22"
131             },
132             {
133                 ID: "14",
134                 hobbit: "听音乐",
135                 sport: "打篮球",
136                 movies: "功夫",
137                 sender: "nlon",
138                 received: "2017-08-18 13:23:21",
139                 sex: "woman",
140                 age: "12"
141             },
142             {
143                 ID: "15",
144                 hobbit: "听音乐",
145                 sport: "打篮球",
146                 movies: "功夫",
147                 sender: "elon",
148                 received: "2017-04-18 13:23:21",
149                 sex: "man",
150                 age: "22"
151             },
152             {
153                 ID: "16",
154                 hobbit: "听音乐",
155                 sport: "打篮球",
156                 movies: "功夫",
157                 sender: "elon",
158                 received: "2017-08-18 13:23:21",
159                 sex: "woman",
160                 age: "64"
161             },
162             {
163                 ID: "17",
164                 hobbit: "听音乐",
165                 sport: "打篮球",
166                 movies: "功夫",
167                 sender: "nelson",
168                 received: "2017-06-18 13:12:21",
169                 sex: "man",
170                 age: "22"
171             },
172             {
173                 ID: "18",
174                 hobbit: "听音乐",
175                 sport: "打篮球",
176                 movies: "功夫",
177                 sender: "lson",
178                 received: "2017-06-18 13:23:21",
179                 sex: "man",
180                 age: "22"
181             },
182             {
183                 ID: "19",
184                 hobbit: "听音乐",
185                 sport: "打篮球",
186                 movies: "功夫",
187                 sender: "n",
188                 received: "2017-08-18 14:23:21",
189                 sex: "woman",
190                 age: "43"
191             },
192             {
193                 ID: "20",
194                 hobbit: "听音乐",
195                 sport: "打篮球",
196                 movies: "功夫",
197                 sender: "on",
198                 received: "2017-08-18 13:13:21",
199                 sex: "man",
200                 age: "22"
201             },
202             {
203                 ID: "21",
204                 hobbit: "听音乐",
205                 sport: "打篮球",
206                 movies: "功夫",
207                 sender: "non",
208                 received: "2017-08-18 16:23:21",
209                 sex: "woman",
210                 age: "15"
211             },
212             {
213                 ID: "22",
214                 hobbit: "听音乐",
215                 sport: "打篮球",
216                 movies: "功夫",
217                 sender: "eon",
218                 received: "2017-08-18 13:23:21",
219                 sex: "man",
220                 age: "22"
221             },
222         ];
223 
224     //正序
225     dataContent = ReverseRankingDate(dataContent, "received");
226     console.log(dataContent);
227 
228     //封装的日期排序方法
229    function ReverseRankingDate(data, p) {
230     for (i = 0; i < data.length - 1; i++) {
231         for (j = 0; j < data.length - 1 - i; j++) {
232             console.log(Date.parse(data[j][p]));
233             if (Date.parse(data[j][p]) < Date.parse(data[j+1][p])) {
234                 var temp = data[j];
235                 data[j] = data[j + 1];
236                 data[j + 1] = temp;
237             }
238         }
239     }
240     return data;
241 }
View Code

 

   这是其他排序的方法,欢迎查看,欢迎给意见!

  其他排序方法如下:

  JavaScript非数字(中文)排序

  JavaScript按纯数字排序

  JavaScript日期排序

  JavaScript按IP地址排序

posted @ 2018-02-11 10:32  小旺同学  阅读(4277)  评论(0编辑  收藏  举报