我正在着手解决电子密码问题(https://leetcode.com/problems/sender- with-largest-word-count/) 当我打过假想时,我花了几个小时试图 de倒,仍然可以 figure出正在发生的事情。 简化,考虑到物体:
let items = [
[ K , 4 ], [ kFIbpoFxn , 10 ], [ yErgn , 6 ],
[ N , 8 ], [ wtJesr , 9 ], [ rusffeL , 14 ],
[ KlpoodEd , 5 ], [ qGcQqIVdFr , 1 ], [ ztmCdK , 10 ],
[ HFILjKln , 12 ], [ TmmQZ , 18 ], [ R , 7 ],
[ CNh , 7 ], [ YMQDBkOWy , 22 ], [ kjiSc , 4 ],
[ cGMsZxxx , 6 ], [ PPqsmNBewN , 21 ], [ gbtn , 10 ],
[ nQNcL , 8 ], [ rK , 10 ], [ ppr , 16 ],
[ LhSVp , 11 ], [ Ub , 7 ], [ QGRFMLY , 11 ],
[ SdDObYkD , 9 ], [ q , 3 ], [ suAakSCuHz , 6 ],
[ dnzhjdwrEt , 8 ], [ ubIEXAO , 22 ], [ EsBuLal , 11 ],
[ xlQqQRrdTv , 12 ], [ mWxCG , 8 ], [ DmwIEmS , 5 ],
[ nBQLLS , 6 ], [ QhF , 4 ], [ bmtYQKYv , 5 ],
[ PRiNk , 7 ], [ QyYJw , 7 ], [ QIFauTN , 3 ],
[ zJLcUq , 13 ], [ TU , 1 ], [ lCkGjDY , 7 ],
[ A , 6 ]
]
I am attempting to sort it based first on highest integer, but if those values are equal, by largest lexicographical value, ie the .reverse() of how JS sorts strings by default
my sorting code is:
items.sort(function(a, b) {
return b[1] !== a[1] ? b[1] - a[1] : -a[0].localeCompare(b[0]);
});
结果是:
[
[ YMQDBkOWy , 22 ], [ ubIEXAO , 22 ], [ PPqsmNBewN , 21 ],
[ TmmQZ , 18 ], [ ppr , 16 ], [ rusffeL , 14 ],
[ zJLcUq , 13 ], [ xlQqQRrdTv , 12 ], [ HFILjKln , 12 ],
[ QGRFMLY , 11 ], [ LhSVp , 11 ], [ EsBuLal , 11 ],
[ ztmCdK , 10 ], [ rK , 10 ], [ kFIbpoFxn , 10 ],
[ gbtn , 10 ], [ wtJesr , 9 ], [ SdDObYkD , 9 ],
[ nQNcL , 8 ], [ N , 8 ], [ mWxCG , 8 ],
[ dnzhjdwrEt , 8 ], [ Ub , 7 ], [ R , 7 ],
[ QyYJw , 7 ], [ PRiNk , 7 ], [ lCkGjDY , 7 ],
[ CNh , 7 ], [ yErgn , 6 ], [ suAakSCuHz , 6 ],
[ nBQLLS , 6 ], [ cGMsZxxx , 6 ], [ A , 6 ],
[ KlpoodEd , 5 ], [ DmwIEmS , 5 ], [ bmtYQKYv , 5 ],
[ QhF , 4 ], [ kjiSc , 4 ], [ K , 4 ],
[ QIFauTN , 3 ], [ q , 3 ], [ TU , 1 ],
[ qGcQqIVdFr , 1 ]
]
这个问题应在YMQDBkOWy之前处理。 当你单独工作时——a.compare Locale(b)正确工作时,在其他测试情况下,该守则的这一部分正确划出其他指示。 但不在此试验中。 我失踪了吗? 谢谢!
(我知道有无数其他办法解决这一问题,我只想知道,为什么这种职能与本目标的目的不一样。)