Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sorting with undefined #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

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