Browse Source

Sort webmentions by datetime rather than API return order

Ryan Rix 2 years ago
parent
commit
54c2033e0e
1 changed files with 12 additions and 5 deletions
  1. 12 5
      webmention.js

+ 12 - 5
webmention.js

@@ -93,11 +93,18 @@ webMention.generateItem = function (index, apiData, callback) {
 }
 
 webMention.listLinks = function (parentElement, apiData) {
-  for (var j = 0; j < apiData.links.length; j++) {
-    webMention.generateItem(j, apiData, function (listItem) {
-      parentElement.insertBefore(listItem, parentElement.firstChild);
-    })
-  }
+    var links = apiData.links.sort(function(a, b) {
+        if(!a.data.published_ts) {return 1;}
+        if(!b.data.published_ts) {return -1;}
+        if (a.data.published_ts < b.data.published_ts) { return 1 };
+        if (a.data.published_ts == b.data.published_ts) { return 0 };
+        if (a.data.published_ts > b.data.published_ts) { return -1 };
+    });
+    for (var j = 0; j < links.length; j++) {
+        webMention.generateItem(j, apiData, function (listItem) {
+            parentElement.insertBefore(listItem, parentElement.firstChild);
+        })
+    }
 }
 
 webMention.showCount = function (parentElement, apiData) {