简体   繁体   中英

How I need to Sorts digit names JS. And I cant use loops. Only standard Array methods

Examples:

 *   [] => []
 *   [ 'nine','one' ]                 => [ 'one', 'nine' ]
 *   [ 'one','two','three' ]          => [ 'one','two', 'three' ]
 *   [ 'nine','eight','nine','eight'] => [ 'eight','eight','nine','nine']
 *   [ 'one','one','one','zero' ]     => [ 'zero','one','one','one' ]
 */

I suppose you don't know how to even start, that's why you didn't even try.

Anyway, here is one of approches, how it can be done:

 const input = ['nine','eight','nine','eight', 'one','two','three'] const digitsComparator = (a, b) => { const mapper = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] return mapper.indexOf(a) - mapper.indexOf(b) } const res = input.sort(digitsComparator) console.log(res)

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