繁体   English   中英

我需要如何对数字名称 JS 进行排序。 而且我不能使用循环。 只有标准的数组方法

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

例子:

 *   [] => []
 *   [ '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' ]
 */

我想你甚至不知道如何开始,这就是你甚至没有尝试的原因。

无论如何,这是一种方法,它是如何完成的:

 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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