简体   繁体   English

将这个嵌套的三元运算提取成一个独立的语句。 声纳问题

[英]Extract this nested ternary operation into an independent statement. Sonar Issue

I'm unable to resolve this sonar issue.我无法解决这个声纳问题。

public sortByPredicate(dataList: any[], predicate: string): any[] {
    return dataList.sort((a: string, b: string) => 
      (a[predicate] > b[predicate]) ? 1 : ((b[predicate] > a[predicate]) ? -1 : 0));

this should be the solution:这应该是解决方案:

  public sortByPredicate(dataList: any[], predicate: string): any[] {
    return dataList.sort((a: string, b: string) => {
      if ((a[predicate] > b[predicate])) {
        return 1;
      } else {
        if (b[predicate] > a[predicate]) {
          return -1;
        } else {
          return 0;
        }
      }
    });
  }

some else are useless, this is a shorter form else一些没用,这是一个较短的形式

  public sortByPredicate3(dataList: any[], predicate: string): any[] {
    return dataList.sort((a: string, b: string) => {
      if ((a[predicate] > b[predicate])) {
        return 1;
      }
      if (b[predicate] > a[predicate]) {
        return -1;
      }
      return 0;
    });
  }

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

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