English 中文(简体)
Java 模范习俗,使用不工作的双阵列物体
原标题:JavaScript custom .sort() of double array object using .localeCompare() not working

我正在着手解决电子密码问题(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)正确工作时,在其他测试情况下,该守则的这一部分正确划出其他指示。 但不在此试验中。 我失踪了吗? 谢谢!

(我知道有无数其他办法解决这一问题,我只想知道,为什么这种职能与本目标的目的不一样。)

问题回答

You can directly use the comparison operators to make it case sensitive.

items.sort((a, b) => b[1] - a[1] || (b[0] > a[0]) - (b[0] < a[0]));




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签