在JavaScript中比较两个数组时,我找不到关于“大于”,“小于”,“LTE”,“GTE”的任何描述或提及。
我能想到的唯一琐碎的事情是,两个数组通过两个元素对每个索引相对索引进行比较,但是在测试之后--我没有得到我期望的结果。
那么数组是如何比较的呢?
少量测试用例:
console.log([1] > [2]); // FALSE - ok
console.log([2] > [1]); // TRUE - ok
console.log([2] > [2]); // FALSE - ok
console.log([1,2] > [2,3]); // FALSE - In case two elements in each index are compared, and the answer is "If all >" - ok
console.log([2,2] > [1,1]); // TRUE - In case two elements in each index are compared, and the answer is "If all >" - ok
console.log([1,2] > [1,1]); // TRUE - In case two elements in each index are compared, and the answer is "If all >" - Unexpected
// More cases with 3 elements:
console.log([1,2,1] > [1,2,2]); // FALSE
console.log([1,1,1] > [1,1,2]); // FALSE
console.log([1,3,1] > [1,3,0]); // TRUE
console.log([1,1,1] > [1,1,2]); // FALSE
通过在比较之前调用Array#ToString
方法将数组转换为字符串,该方法的作用与Array#Join
相同,不带参数。
字符串比较通过顺序比较两个字符串的每个字符的代码点来工作。