From 30e6182c7a10a84683f42a9099431654cea4a1fa Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 23 Feb 2021 12:42:17 -0800 Subject: [PATCH] sort game names in opengames dialog --- xwords4/wasm/xwutils.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/xwords4/wasm/xwutils.js b/xwords4/wasm/xwutils.js index 83e7c6483..487f5955c 100644 --- a/xwords4/wasm/xwutils.js +++ b/xwords4/wasm/xwutils.js @@ -186,22 +186,31 @@ function nbBlankPick(title, buttons, proc, closure) { addDepthNote(dlg); } +// To enable sorting of names on buttons while keeping existing code, +// I'm creating an array of pairs then sorting that. function nbGamePick(title, gameMap, proc, closure) { let dlg = newDlgWMsg( title ); - let buttons = []; - let keys = []; + let pairs = []; Object.keys(gameMap).forEach( function(key) { - buttons.push(gameMap[key]); - keys.push(key); + pairs.push({id:key, txt:gameMap[key]}); }); + + pairs.sort(function(a, b){ + var stra = a.txt.toLowerCase(); + var strb = b.txt.toLowerCase(); + if (stra < strb) {return -1;} + if (stra > strb) {return 1;} + return parseInt(a.id) - parseInt(b.id); + }); + let buttons = []; + for ( pair of pairs ) { + buttons.push(pair.txt); + } butProc = function(indx) { dlg.parentNode.removeChild(dlg); - console.log('nbGamePick.proc(' + indx + ')'); - let key = keys[indx]; - console.log('key: ' + key); - ccallString(proc, closure, key); + ccallString(proc, closure, pairs[indx].id); } dlg.appendChild( newButtonDiv( buttons, butProc ) );