admin管理员组文章数量:1323348
I'm having timestamp value in my json response and is able to parse that value properly. Each image is having its own timestamp value and I need to display it in the increasing timestamp value. That means the last updated image should be loaded first in my application.
The timestamp is in the following format in the JSON Response :
Brand1{
"FileName": "520120427043622011.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
}
Brand2{
"FileName": "Kung_Fu_Fingers.jpg",
"UpdateTimeStamp": "Jun 5, 2013 6:51:12 AM"
}
Brand3{
"FileName": "google_logo1.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
}
Can any one please guide me to sort the above three brands images item based on the timestamp value using Javascript or jQuery functions. I have used sort function after spliting the date value, but it is sorting the months in alphabetical order only. Please help me out in finding a solution for this.
Thanks in advance.
I'm having timestamp value in my json response and is able to parse that value properly. Each image is having its own timestamp value and I need to display it in the increasing timestamp value. That means the last updated image should be loaded first in my application.
The timestamp is in the following format in the JSON Response :
Brand1{
"FileName": "520120427043622011.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
}
Brand2{
"FileName": "Kung_Fu_Fingers.jpg",
"UpdateTimeStamp": "Jun 5, 2013 6:51:12 AM"
}
Brand3{
"FileName": "google_logo1.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
}
Can any one please guide me to sort the above three brands images item based on the timestamp value using Javascript or jQuery functions. I have used sort function after spliting the date value, but it is sorting the months in alphabetical order only. Please help me out in finding a solution for this.
Thanks in advance.
Share Improve this question edited Jul 9, 2024 at 9:52 Abin Koshy Cheriyan asked Jun 10, 2013 at 14:35 Abin Koshy CheriyanAbin Koshy Cheriyan 6177 silver badges21 bronze badges 3- Is this ing in an array ?. Please share plete JSON. – Diode Commented Jun 10, 2013 at 14:45
- { "FileName": "520120427043622011.jpg", "FileUrl": "myserver./files/basic/anonymous/api/library/…", "MimeType": "image/jpeg", "TagName": "my_thumb", "FileLength": "243319", "FileDescription": "test", "UpdateTimeStamp": "Jun 10, 2013 7:33:02 AM" }, – Abin Koshy Cheriyan Commented Jun 10, 2013 at 14:47
- I'm able to parse the response. For sorting based on timestamp value I need help.I have stored the timestamp value in an array.Need to sort that array containing timestamp value. – Abin Koshy Cheriyan Commented Jun 10, 2013 at 14:48
2 Answers
Reset to default 4var files = [{
"FileName": "520120427043622011.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
},{
"FileName": "Kung_Fu_Fingers.jpg",
"UpdateTimeStamp": "Jun 5, 2013 6:51:12 AM"
},{
"FileName": "google_logo1.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
}];
files.sort(function(a, b){
var d1 = new Date(a.UpdateTimeStamp);
var d2 = new Date(b.UpdateTimeStamp);
return d1-d2; // d2-d1 for descending order
});
EDIT:
For array of timestamps
var updateTimeStamps = ["Aug 10, 2013 8:31:23 AM","Jun 5, 2013 6:51:12 AM" ,"May 10, 2013 8:31:23 AM"];
updateTimeStamps.sort(function(a, b){
var d1 = new Date(a);
var d2 = new Date(b);
return d1-d2; // d2-d1 for descending order
});
console.log(updateTimeStamps);
EDIT 2: For keeping two arrays and sorting
var imgs = ["img1", "img2", "img3"];
var times = ["Jul 10, 2013 8:31:23 AM","Jan 5, 2013 6:51:12 AM" ,"Feb 10, 2013 8:31:23 AM"];
var sorting = [];
times.sort(function(a, b){
var d1 = new Date(a);
var d2 = new Date(b);
var d = d1-d2; // d2-d1 for descending order
sorting.push(d);
return d;
});
imgs.sort(function(a, b){
return sorting.shift();
});
console.log(times);
console.log(imgs);
you can define insertion-sort, that executes in O(n)-time (best-case, array is sorted), and O(n^2) array is not sorted (worst case):
function insertionSort(files,attrToSortBy){
for(var k=1; k < files.length; k++){
for(var i=k; i > 0 && new Date(files[i][attrToSortBy]) <
new Date(files[i-1][attrToSortBy]); i--){
var tmpFile = files[i];
files[i] = files[i-1];
files[i-1] = tmpFile;
}
}
}
var files = [{
"FileName": "520120427043622011.jpg",
"UpdateTimeStamp": "Jun 10, 2013 8:31:23 AM"
},{
"FileName": "Kung_Fu_Fingers.jpg",
"UpdateTimeStamp": "Jun 5, 2013 6:51:12 AM"
},{
"FileName": "google_logo1.jpg",
"UpdateTimeStamp": "Jun 12, 2013 8:31:23 AM"
}];
insertionSort(files,"UpdateTimeStamp");
console.log('files: ',files);
本文标签: javascriptSorting an array based on time stamp value from JSON ResponseStack Overflow
版权声明:本文标题:javascript - Sorting an array based on time stamp value from JSON Response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742133494a2422268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论