Skip to content

Snapshot browser does not yet use natural sort order #1035

@yhdsl

Description

@yhdsl

Describe the bug

When sorting file names, natural order sorting is expected, where numbers are sorted as 1 > 2 > 10. For special characters, such as Chinese or Japanese characters, pinyin-based sorting should be applied.

To Reproduce

Steps to reproduce the behavior:

Create the following file list:
Image
Perform a backup and view the snapshot browser.

Expected behavior

Expect correct sorting, but the actual result is
Image

Platform Info

  • OS and Architecture [Windows 11 x64]
  • Backrest Version [e.g. 1.10.1]

Additional context

Intl.Collator('en', {numeric: true, caseFirst: 'lower'}) is sufficient to resolve most sorting issues. However, in tests, it lacks pinyin-based sorting for Chinese or Japanese characters. Therefore, it is necessary to set the locale to 'zh' specifically for these characters. (If the 'zh' locale is applied universally, English characters will also be sorted according to pinyin.)

Here is a working fix, but it was written in a somewhat hasty manner.

// SnapshotBrowser.tsx

function compareFn(a: DataNode, b: DataNode) {
  if (a.isLeaf === true && b.isLeaf === false) {
    return 1;
  }
  else if (a.isLeaf === false && b.isLeaf === true) {
    return -1;
  }
  else {
    const isChineseA = /[\u4e00-\u9fa5]/.test(a.key.toString());
    const isChineseB = /[\u4e00-\u9fa5]/.test(b.key.toString());

    if (isChineseA && isChineseB) {
      return a.key.toString().localeCompare(b.key.toString(), 'zh', {numeric: true, caseFirst: 'lower'});
    } else if (!isChineseA && !isChineseB) {
      return a.key.toString().localeCompare(b.key.toString(), 'en', {numeric: true, caseFirst: 'lower'});
    } else {
      return isChineseA ? 1 : -1;
    }
  }
}

...
  const respToNodes = (resp: ListSnapshotFilesResponse): DataNode[] => {
...
    nodes.sort(compareFn);
    return nodes;
  };
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions