简体   繁体   English

Typecript中排序错误的类型

[英]Wrong type with sort in Typescript

var cheapest = leaves.sort((a,b) => <boolean>(<number>a.cost < <number>b.cost));
//also tried without casting

Gives me the following error: 给我以下错误:

'Error'
message: 'Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type '(a: any, b: any) => number'.
Type 'boolean' is not assignable to type 'number'.'

How should i fix this? 我该如何解决这个问题?

Edit: The js code ( original) is taken from : https://github.com/atomicptr/goap/blob/gh-pages/gameplay/ai/planner.js , which indeed seems to sort by bool instead of number. 编辑:js代码(原始)取自: https//github.com/atomicptr/goap/blob/gh-pages/gameplay/ai/planner.js ,它确实似乎按bool而不是数字排序。

This is not how Array.sort works. 这不是Array.sort的工作原理。 You need to return a number, but the predicate you've given returns a boolean (The less than ( < ) operator results in true or false). 您需要返回一个数字,但是您给出的谓词返回一个布尔值(小于( < )运算符导致true或false)。 The sort order is determined by whether the number your function returns is negative, positive, or zero. 排序顺序取决于函数返回的数字是负数,正数还是零。 The MDN example illustrates this well with an example compare function. MDN示例通过示例比较函数说明了这一点。

function compare(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
}

If you want to sort ascending you can do this with 如果你想升序排序,你可以这样做

var cheapest = leaves.sort((a,b) => a.cost - b.cost);

assuming that leaves is typed correctly so a and b have their types correctly inferred. 假设leaves的输入正确,因此ab的类型正确推断。

The comparator function of a sort should return -1, 0, or 1. Returning a boolean instead (effectively 1 or 0) will fail to sort the list correctly! 排序的比较器函数应该返回-1,0或1.返回布尔值(实际上是1或0)将无法正确排序列表!

For example, in Chrome (sort implementations are host-dependent), this line: 例如,在Chrome中(排序实现与主机相关),此行:

[1, 2, 5, 6, 5, 5, 4, 3, 2, 1, 4, 2, 4, 6, 3].sort(function(a, b) { return a < b; })

Produces: 生产:

[3, 3, 5, 6, 5, 5, 4, 6, 4, 4, 2, 2, 2, 1, 1]

Which is not sorted! 哪个没有排序!

You should write something like a > b ? 1 : a === b ? 0 : -1 你应该写一个像a > b ? 1 : a === b ? 0 : -1东西a > b ? 1 : a === b ? 0 : -1 a > b ? 1 : a === b ? 0 : -1 a > b ? 1 : a === b ? 0 : -1 instead a > b ? 1 : a === b ? 0 : -1代替

When using Array.prototype.sort(), a comparator function can be used to properly define sorting behaviour. 使用Array.prototype.sort()时,可以使用比较器函数来正确定义排序行为。 According to the MDN Javascript documentation: 根据MDN Javascript文档:

To compare numbers instead of strings, the compare function can simply subtract b from a. 要比较数字而不是字符串,比较函数可以简单地从a中减去b。

Thus to sort the array 'leaves' in ascending order: 因此,按升序对数组“叶子”进行排序:

var cheapest = leaves.sort((a,b) => a.cost- b.cost);

With a Typescript generic: 使用Typescript通用:

var cheapest = leaves.sort((a,b) => <number>(a.cost- b.cost));

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

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