Sort until we can't sort anymore

This commit is contained in:
Jonathan Putney
2020-07-30 15:53:50 -04:00
parent 0f9143be46
commit dcad088b25
4 changed files with 56 additions and 21 deletions

37
dist/scorm-again.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -889,15 +889,16 @@ export default class BaseAPI {
return null; return null;
} }
const int_pattern = /^(cmi\.interactions\.)(\d+)\.(.*)$/; /**
const obj_pattern = /^(cmi\.objectives\.)(\d+)\.(.*)$/; * Function to sort the array, which we'll call until we're done.
*
const result = Object.keys(json).map(function(key) { * @param {string} a
return [String(key), json[key]]; * @param {string} b
}); * @param {string} c
* @param {string} d
// CMI interactions need to have id and type loaded before any other fields * @return {number}
result.sort(function([a, b], [c, d]) { */
function resultSort([a, b], [c, d]) {
let test; let test;
if ((test = testPattern(a, c, int_pattern)) !== null) { if ((test = testPattern(a, c, int_pattern)) !== null) {
return test; return test;
@@ -913,10 +914,25 @@ export default class BaseAPI {
return 1; return 1;
} }
return 0; return 0;
}
const int_pattern = /^(cmi\.interactions\.)(\d+)\.(.*)$/;
const obj_pattern = /^(cmi\.objectives\.)(\d+)\.(.*)$/;
const result = Object.keys(json).map(function(key) {
return [String(key), json[key]];
}); });
// CMI interactions need to have id and type loaded before any other fields
// Call the sort until everything is properly sorted.
// I must be missing an edge case in my sort?
let sorted_result = [];
while (sorted_result !== result.sort(resultSort)) {
sorted_result = result;
}
let obj; let obj;
result.forEach((element) => { sorted_result.forEach((element) => {
obj = {}; obj = {};
obj[element[0]] = element[1]; obj[element[0]] = element[1];
this.loadFromJSON(unflatten(obj), CMIElement); this.loadFromJSON(unflatten(obj), CMIElement);