简体   繁体   中英

How to create custom alphabetical order in JavaScript?

I have a custom alphabetical order for a custom script I've written. It is defined in the following code, as well as my attempt at specifying an alphabetize algorithm, which isn't working.

 const symbols = [ { ascii: "i", unicode: "\Ѱ" }, { ascii: "a", unicode: "\Ԁ" }, { ascii: "u", unicode: "\ϰ" }, { ascii: "e", unicode: "\А" }, { ascii: "E", unicode: "\ѐ" }, { ascii: "U", unicode: "\Ҡ" }, { ascii: "I", unicode: "\ϐ" }, { ascii: "o", unicode: "\Ӡ" }, { ascii: "A", unicode: "\ΰ" }, { ascii: "O", unicode: "\Ӏ" }, { ascii: "o#", unicode: "\ΐ" }, { ascii: "u#", unicode: "\а" }, { ascii: "e#", unicode: "\ё" }, { ascii: "i#", unicode: "\ϑ" }, { ascii: "a#", unicode: "\ԁ" }, { ascii: "m", unicode: "\Ā" }, { ascii: "n", unicode: "\ŀ" }, { ascii: "q", unicode: "\Š" }, { ascii: "g", unicode: "\İ" }, { ascii: "d", unicode: "\`" }, { ascii: "b", unicode: "\@" }, { ascii: "p", unicode: "\0" }, { ascii: "t", unicode: "\Ð" }, { ascii: "k", unicode: "\P" }, { ascii: "h", unicode: "\Ġ" }, { ascii: "l", unicode: "\Ű" }, { ascii: "w", unicode: "\Đ" }, { ascii: "f", unicode: "\À" }, { ascii: "s", unicode: "\p" }, { ascii: "C", unicode: "\°" }, { ascii: "z", unicode: "\" }, { ascii: "v", unicode: "\ð" }, { ascii: "y", unicode: "\ƀ" }, { ascii: "x", unicode: "\Ɛ" }, { ascii: "r", unicode: "\à" }, { ascii: "c", unicode: "\€" }, { ascii: "j", unicode: "\Ő" }, { ascii: "Q", unicode: "\Ơ" }, { ascii: "S", unicode: "\r" }, { ascii: "Z", unicode: "\’" }, { ascii: "H", unicode: "\Ģ" }, { ascii: "'", unicode: "\ư" }, ] const alphabet = symbols.map(o => o.ascii) const words = [ 'He', 'Hix', 'Hex', 'SoHiz', 'SoH', 'xo', 'SoHi', 'Hi', 'CU' ] const alphabetize = (array, alphabet) => { return array.sort((a, b) => { for (let i = 0, n = a.length; i < n; i++) { let x = a[i] let y = b[i] if (y) { let diff = alphabet.indexOf(x) - alphabet.indexOf(y) if (diff) return diff } else { return -1 } } }) } console.log(alphabetize(words, alphabet).join('\\n'))

The output should be:

CU
xo
SoH
SoHi
SoHiz
Hi
Hix
He
Hex

What am I doing wrong here, and how can this become optimal?

Two things:

  • if b[i] is undefined, then the return value should not be -1, but 1: this will put the shorter b before the longer a .

  • There is a possibility that the for loop will complete without a return exit. In that case it could be that a and b are equal or b is longer. You need to return a good value for that case.

Here is a correction:

 const symbols = [{ ascii: "i", unicode: "\Ѱ" },{ ascii: "a", unicode: "\Ԁ" },{ ascii: "u", unicode: "\ϰ" },{ ascii: "e", unicode: "\А" },{ ascii: "E", unicode: "\ѐ" },{ ascii: "U", unicode: "\Ҡ" },{ ascii: "I", unicode: "\ϐ" },{ ascii: "o", unicode: "\Ӡ" },{ ascii: "A", unicode: "\ΰ" },{ ascii: "O", unicode: "\Ӏ" },{ ascii: "o#", unicode: "\ΐ" },{ ascii: "u#", unicode: "\а" },{ ascii: "e#", unicode: "\ё" },{ ascii: "i#", unicode: "\ϑ" },{ ascii: "a#", unicode: "\ԁ" },{ ascii: "m", unicode: "\Ā" },{ ascii: "n", unicode: "\ŀ" },{ ascii: "q", unicode: "\Š" },{ ascii: "g", unicode: "\İ" },{ ascii: "d", unicode: "\`" },{ ascii: "b", unicode: "\@" },{ ascii: "p", unicode: "\0" },{ ascii: "t", unicode: "\Ð" },{ ascii: "k", unicode: "\P" },{ ascii: "h", unicode: "\Ġ" },{ ascii: "l", unicode: "\Ű" },{ ascii: "w", unicode: "\Đ" },{ ascii: "f", unicode: "\À" },{ ascii: "s", unicode: "\p" },{ ascii: "C", unicode: "\°" },{ ascii: "z", unicode: "\" },{ ascii: "v", unicode: "\ð" },{ ascii: "y", unicode: "\ƀ" },{ ascii: "x", unicode: "\Ɛ" },{ ascii: "r", unicode: "\à" },{ ascii: "c", unicode: "\€" },{ ascii: "j", unicode: "\Ő" },{ ascii: "Q", unicode: "\Ơ" },{ ascii: "S", unicode: "\r" },{ ascii: "Z", unicode: "\’" },{ ascii: "H", unicode: "\Ģ" },{ ascii: "'", unicode: "\ư" },]; const alphabet = symbols.map(o => o.ascii); const words = ['He','Hix','Hex','SoHiz','SoH','xo','SoHi','Hi','CU'] const alphabetize = (array, alphabet) => { return array.sort((a, b) => { for (let i = 0, n = a.length; i < n; i++) { let x = a[i]; let y = b[i]; if (y) { let diff = alphabet.indexOf(x) - alphabet.indexOf(y); if (diff) return diff; } else { return 1; // <-- corrected } } return a.length - b.length; // is 0 or negative }) } console.log(alphabetize(words, alphabet).join('\\n'));

You need to account for when either word is longer than the other

 const symbols = [ { ascii: "i", unicode: "\Ѱ" }, { ascii: "a", unicode: "\Ԁ" }, { ascii: "u", unicode: "\ϰ" }, { ascii: "e", unicode: "\А" }, { ascii: "E", unicode: "\ѐ" }, { ascii: "U", unicode: "\Ҡ" }, { ascii: "I", unicode: "\ϐ" }, { ascii: "o", unicode: "\Ӡ" }, { ascii: "A", unicode: "\ΰ" }, { ascii: "O", unicode: "\Ӏ" }, { ascii: "o#", unicode: "\ΐ" }, { ascii: "u#", unicode: "\а" }, { ascii: "e#", unicode: "\ё" }, { ascii: "i#", unicode: "\ϑ" }, { ascii: "a#", unicode: "\ԁ" }, { ascii: "m", unicode: "\Ā" }, { ascii: "n", unicode: "\ŀ" }, { ascii: "q", unicode: "\Š" }, { ascii: "g", unicode: "\İ" }, { ascii: "d", unicode: "\`" }, { ascii: "b", unicode: "\@" }, { ascii: "p", unicode: "\0" }, { ascii: "t", unicode: "\Ð" }, { ascii: "k", unicode: "\P" }, { ascii: "h", unicode: "\Ġ" }, { ascii: "l", unicode: "\Ű" }, { ascii: "w", unicode: "\Đ" }, { ascii: "f", unicode: "\À" }, { ascii: "s", unicode: "\p" }, { ascii: "C", unicode: "\°" }, { ascii: "z", unicode: "\" }, { ascii: "v", unicode: "\ð" }, { ascii: "y", unicode: "\ƀ" }, { ascii: "x", unicode: "\Ɛ" }, { ascii: "r", unicode: "\à" }, { ascii: "c", unicode: "\€" }, { ascii: "j", unicode: "\Ő" }, { ascii: "Q", unicode: "\Ơ" }, { ascii: "S", unicode: "\r" }, { ascii: "Z", unicode: "\’" }, { ascii: "H", unicode: "\Ģ" }, { ascii: "'", unicode: "\ư" }, ] const alphabet = symbols.map(o => o.ascii) const words = [ 'He', 'Hix', 'Hex', 'SoHiz', 'SoH', 'xo', 'SoHi', 'Hi', 'CU' ] const alphabetize = (array, alphabet) => { return array.sort((a, b) => { for (let i = 0, n = a.length; i < n; i++) { let x = a[i] let y = b[i] if (y && x) { let diff = alphabet.indexOf(x) - alphabet.indexOf(y) if (diff) return diff } else if (y) { return -1 } else { return 1 } } }) } console.log(alphabetize(words, alphabet).join('\\n'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM