最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sort JSON data based on Date and Time - Stack Overflow

programmeradmin0浏览0评论

Is it possible to rearrange the below JSON format based on time (2016-12-07T13:00:00) using jQuery/Javascript.

[
   {
      "id":1,
      "start":"2016-12-07T13:00:00",
      "subject":"test1",
   },
   {
      "id":2,
      "start":"2016-12-07T09:00:00",
      "subject":"test2",
   },
   {
      "id":3,
      "start":"2016-12-07T10:00:00",
      "subject":"test3",
   },
   {
      "id":4,
      "start":"2016-12-07T07:00:00",
      "subject":"test4",
   },
   {
      "id":5,
      "start":"2016-12-07T14:00:00",
      "subject":"test5",
   }
]

Is it possible to rearrange the below JSON format based on time (2016-12-07T13:00:00) using jQuery/Javascript.

[
   {
      "id":1,
      "start":"2016-12-07T13:00:00",
      "subject":"test1",
   },
   {
      "id":2,
      "start":"2016-12-07T09:00:00",
      "subject":"test2",
   },
   {
      "id":3,
      "start":"2016-12-07T10:00:00",
      "subject":"test3",
   },
   {
      "id":4,
      "start":"2016-12-07T07:00:00",
      "subject":"test4",
   },
   {
      "id":5,
      "start":"2016-12-07T14:00:00",
      "subject":"test5",
   }
]
Share Improve this question edited Dec 8, 2016 at 9:35 Mohammad Sadiqur Rahman 5,5538 gold badges33 silver badges47 bronze badges asked Dec 8, 2016 at 8:14 Vijay BaskaranVijay Baskaran 9592 gold badges10 silver badges19 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

You could use String#localeCompare in a sort callback for the property start, because ISO 8601 dates are sortable as string.

var array = [
    { id: 1, start: "2016-12-07T13:00:00", subject: "test1" },
    { id: 2, start: "2016-12-07T09:00:00", subject: "test2" },
    { id: 3, start: "2016-12-07T10:00:00", subject: "test3" },
    { id: 4, start: "2016-12-07T07:00:00", subject: "test4" },
    { id: 5, start: "2016-12-07T14:00:00", subject: "test5" }
];

array.sort(function (a, b) {
    return a.start.localeCompare(b.start);
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

//put to variable
var db = [
   {
      "id":1,
      "start":"2016-12-07T13:00:00",
      "subject":"test1",
   },
   {
      "id":2,
      "start":"2016-12-07T09:00:00",
      "subject":"test2",
   },
   {
      "id":3,
      "start":"2016-12-07T10:00:00",
      "subject":"test3",
   },
   {
      "id":4,
      "start":"2016-12-07T07:00:00",
      "subject":"test4",
   },
   {
      "id":5,
      "start":"2016-12-07T14:00:00",
      "subject":"test5",
   }
];

//use .sort()
db.sort(
  function(a,b){ 
    
    //use new Date parse string to date type
    //convert date to number use Date.parse()
    //format function(a,b) { return a-b; }
    
    return Date.parse(new Date(a.start)) - Date.parse(new Date(b.start)); 
  }
);
console.log(db)

If You can use External Library For example Lodash

https://lodash./

Assign the json string to variable and try

var sort = _.sortBy(variable_name, "start")

hope this Helps you.

Yes, you would use the javascript array sort method ( http://www.w3schools./jsref/jsref_sort.asp ).

Define your array as assign it to an variable then call the sort method, you can pass the sort method a function to perform the detail of the parison.

    var aryData = [{"id":1
                   ,"start":"2016-12-07T13:00:00"
                   ,"subject":"test1"
                   },{
                    "id":2
                   ,"start":"2016-12-07T09:00:00"
                   ,"subject":"test2"
                   },{
                    "id":3
                   ,"start":"2016-12-07T10:00:00"
                   ,"subject":"test3"
                   },{
                    "id":4
                   ,"start":"2016-12-07T07:00:00"
                   ,"subject":"test4"
                   },{
                    "id":5
                   ,"start":"2016-12-07T14:00:00"
                   ,"subject":"test5"
                   }];
    aryData.sort(function(a, b) {
        var dtA = new Date(a['start'])
           ,dtB = new Date(b['start']);
        return ( dtA.getTime() - dtB.getTime() );
    });

Try this maybe helpful,

var aryData =[
   {
      "id":1,
      "start":"2016-12-07T13:00:00",
      "subject":"test1",
   },
   {
      "id":2,
      "start":"2016-12-07T09:00:00",
      "subject":"test2",
   },
   {
      "id":3,
      "start":"2016-12-07T10:00:00",
      "subject":"test3",
   },
   {
      "id":4,
      "start":"2016-12-07T07:00:00",
      "subject":"test4",
   },
   {
      "id":5,
      "start":"2016-12-07T14:00:00",
      "subject":"test5",
   }
];
    function p(a, b) {
        return new Date(a.start).getTime() - new Date(b.start).getTime();
    }
    aryData.sort(p);

console.log(aryData);

发布评论

评论列表(0)

  1. 暂无评论