testing js sorting with non-latin characters
Last active
January 12, 2017 21:43
-
-
Save rococodogs/8469bb0c8f55c0858d78f92ad0926c85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| </head> | |
| <body> | |
| <input type="text"/> | |
| <div class="list"></div> | |
| <!-- globalize module.exports -->x | |
| <script>var module = {exports: {}}</script> | |
| <script src="strings.js"></script> | |
| <script> | |
| var jp = module.exports.jp | |
| var list = jp | |
| var container = document.querySelector('.list') | |
| var input = document.querySelector('input[type="text"]') | |
| var addTerms = function (list, container) { | |
| container.innerHTML = '' | |
| list.forEach(function (term) { | |
| var el = document.createElement('div') | |
| el.textContent = term | |
| container.appendChild(el) | |
| }) | |
| } | |
| addTerms(list, container) | |
| input.addEventListener('keyup', function (ev) { | |
| var val = this.value.toLowerCase() | |
| var updated = list.filter(function (item) { | |
| return item.toLowerCase().indexOf(val) > -1 | |
| }) | |
| addTerms(updated, container) | |
| }) | |
| </script> | |
| </body> | |
| </html> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sortStrDesc (a, b) { | |
| var aa = a.toLowerCase() | |
| var bb = b.toLowerCase() | |
| if (aa === bb) | |
| return 0 | |
| return aa < bb ? 1 : -1 | |
| } | |
| function sortStrAsc (a, b) { | |
| var aa = a.toLowerCase() | |
| var bb = b.toLowerCase() | |
| if (aa === bb) | |
| return 0 | |
| return bb < aa ? 1 : -1 | |
| } | |
| function sortLocAsc (a, b) { | |
| var comp = String.prototype.localeCompare.call(a, b) | |
| if (comp === 0) | |
| return comp | |
| return comp > 0 ? 1 : -1 | |
| } | |
| function sortLocDesc (a, b) { | |
| var comp = String.prototype.localeCompare.call(a, b) | |
| if (comp === 0) | |
| return 0 | |
| return comp < 0 ? 1 : -1 | |
| } | |
| function sortColAsc (a, b) { | |
| var col = new Intl.Collator('') | |
| } | |
| module.exports = { | |
| str: { | |
| asc: sortStrAsc, | |
| desc: sortStrDesc, | |
| }, | |
| loc: { | |
| asc: sortLocAsc, | |
| desc: sortLocDesc, | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports.jp = [ | |
| 'あ', | |
| '間', | |
| 'アジア', | |
| 'ミケーラ', | |
| 'ネクタイ', | |
| 'やれやれ', | |
| '東京', | |
| '北海道絵葉書會発行', | |
| '科学技術', | |
| '東京大学', | |
| '昭和時代', | |
| '昭和館', | |
| '休み', | |
| '昨日', | |
| '食べたものがすべて', | |
| 'ハサミ', | |
| '鋏', | |
| 'お母さん', | |
| '狼', | |
| 'パソコン', | |
| 'アイヌ民族', | |
| '満州事変', | |
| '浅草寺', | |
| '戦争', | |
| 'ふ', | |
| '府民', | |
| '付箋', | |
| 'ホッチキス', | |
| '学校生活', | |
| ] | |
| module.exports.en = [ | |
| 'dolphin', | |
| 'lion', | |
| 'cat', | |
| 'zebra', | |
| 'bear', | |
| 'GIRAFFE', | |
| 'antelope', | |
| 'PIG', | |
| 'deer', | |
| 'ferret', | |
| 'penguin', | |
| 'puffin', | |
| 'BIRD', | |
| 'brontosaurus', | |
| 'SLOTH', | |
| 'goat', | |
| 'koala', | |
| 'pony', | |
| 'unicorn', | |
| 'tyrannosaurus', | |
| 'triceratops', | |
| 'owl', | |
| 'sloth baby', | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var sorts = require('./sorts') | |
| var strs = require('./strings') | |
| var logTerm = function (term) { console.log('- ' + term) } | |
| // loc || str | |
| var type = process.argv[2] || 'str' | |
| // jp || en | |
| var which = process.argv[3] || 'jp' | |
| var ascFunc = sorts[type].asc | |
| var descFunc = sorts[type].desc | |
| console.log('ascending [%s]', type) | |
| console.log('---------') | |
| strs[which].sort(ascFunc).forEach(logTerm) | |
| console.log('') | |
| console.log('descending [%s]', type) | |
| console.log('----------') | |
| strs[which].sort(descFunc).forEach(logTerm) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment