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