Sort fields alphabetically without losing field information

Change-Id: I60dd6368966ac1108707e88a0573f0228c8e1ad2
diff --git a/dev/js/src/match/meta.js b/dev/js/src/match/meta.js
index d4b2d7f..d4e481e 100644
--- a/dev/js/src/match/meta.js
+++ b/dev/js/src/match/meta.js
@@ -45,24 +45,23 @@
 
       this._element = metaDL;
 
-      var fields = this._fields;
+      let fields = this._fields;
 
-      // TODO:
-      // This should only remember array positions by index
-      // and keep all other field information intact
-      var metaInfo = {};
-      for (var i in fields) {
-        var value = fields[i].value;      	
-        metaInfo[fields[i].key] = value;	
+      // Copy original array position to object
+      // before sorting by key title
+      let posInMetaArray = {};
+      for (let i = 0; i < fields.length; i++) {
+        posInMetaArray[fields[i]["key"]] = i;
+
       };
       
-      // console.log(fields);   
-      
       // TODO: Meta fields should be separated
-      var keys = Object.keys(metaInfo);  
-      
-      for (var i in keys.sort()) {
-        var k = keys[i];
+      const keys = Object.keys(posInMetaArray);
+
+      // Sort all meta keys alphabetically
+      for (let i in keys.sort()) {
+        let k = keys[i];                             // This is the title
+        let field = fields[posInMetaArray[keys[i]]]; // This is the object
 
         // Ignore internal IDs
         if (k !== "UID" &&
@@ -75,13 +74,13 @@
             */
             k !== "layerInfos") {
 
-          var metaL = document.createElement('div');
+          const metaL = document.createElement('div');
           
-          var dt = metaL.addE('dt');
+          const dt = metaL.addE('dt');
           dt.addT(k);
           dt.setAttribute("title", k);
           
-          var metaDescr = metaInfo[k];
+          let metaDescr = field["value"];
           metaDD =  metaL.addE('dd');
           
           if(metaDescr instanceof Array){
@@ -91,14 +90,16 @@
         	  } 
           }
           else{
-            metaDD.addT(metaInfo[k]);
+            metaDD.addT(field["value"]);
           }
           
           metaDL.appendChild(metaL);
         };
       };
 
+      // Add corpusByMatch assistant
       this._corpusByMatch = cbmClass.create(this._element);
+
       return this._element;
     }
   };