Skip to content

sorting with undefined#13

Open
freakypie wants to merge 1 commit intokvnneff:masterfrom
freakypie:master
Open

sorting with undefined#13
freakypie wants to merge 1 commit intokvnneff:masterfrom
freakypie:master

Conversation

@freakypie
Copy link

No description provided.

@Lionad-Morotar
Copy link

undefined 和非 undefined 之间没有可比较性,所以不应该为它们指定比较顺序。

我建议用以下思路重构代码,把排序方法通过入参传进来,同时保留默认的字典排序方法。

// ...

const sortRule = type('function')(args.rule)
  ? args.rule
  : {
     dictionary: (a, b) => a === b ? 0 : ( a< b ? -1 : 1 ),
     number: (a, b) => +a === +b ? 0 : ( +a < +b ? -1 : 1 ),
  }[args.rule || 'dictionary']

result = sort(properties[i], map)(sortRule)(obj1, obj2);

// ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants