Skip to content

Commit

Permalink
fix: default sort styles by localeCompare
Browse files Browse the repository at this point in the history
  • Loading branch information
northword committed Jun 19, 2024
1 parent a2fb100 commit c2c4600
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/styles/data/styles.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ export default {
load(watchedFiles: string[]) {
// watchFiles 是一个所匹配文件的绝对路径的数组。

return watchedFiles.map((file) => {
return fs.readJsonSync(file);
});
return watchedFiles
.map((file) => {
return fs.readJsonSync(file) as Style;
})
.sort((a, b) => {
// title 中包含 GB 的始终最前,否则按预览顺序排序
if (a.title.match("GB")) return -1;
if (b.title.match("GB")) return 1;
return a.title.localeCompare(b.title);
});
},
};

3 comments on commit c2c4600

@zepinglee
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前的结果是 GB 部分随机排序,非 GB 部分按照 Unicode 排序。

Screenshot 2024-06-19 at 19 11 35 Screenshot 2024-06-19 at 19 12 01

改成两个群组内部分别按照拼音排序更好些吧?不确定下面的代码能否 work。

const collator = Intl.Collator("zh", { numeric: true });

...

    return watchedFiles
      .map((file) => {
        return fs.readJsonSync(file) as Style;
      })
      .sort((a, b) => {
        // title 中包含 GB 的始终最前,否则按预览顺序排序
        if (a.title.match("GB")) {
          if (b.title.match("GB")) {
            return collator.compare(a.title, b.title);
          } else {
            return -1;
          }
        } else {
          if (b.title.match("GB")) {
            return 1;
          } else {
            return collator.compare(a.title, b.title);
          }
        }
      });

@northword
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,这个是个 bug,上线的效果和我本地的结果差很多,#67 修改。

@zepinglee
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,这个是个 bug,上线的效果和我本地的结果差很多,#67 修改。

可能是你本地默认 locale 是 zh-CN,跟服务器不一样。

Please sign in to comment.