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

[tree-view] Use common comparator function by sort methods #1195

Open
wants to merge 1 commit into
base: updated-latest-electron
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions packages/tree-view/lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class Directory {
} catch (error) {
names = []
}
names.sort(new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}).compare)
names.sort(this.compareEntries)

const files = []
const directories = []
Expand Down Expand Up @@ -335,16 +335,12 @@ class Directory {
return this.sortEntries(directories.concat(files))
}

normalizeEntryName(value) {
let normalizedValue = value.name
if (normalizedValue == null) {
normalizedValue = value
}
compareEntries(firstName, secondName) {
return compareFn(firstName, secondName)
}

if (normalizedValue != null) {
normalizedValue = normalizedValue.toLowerCase()
}
return normalizedValue
normalizeEntryName(value) {
return value.name ? value.name : value
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we not need to convert to lowercase? Does Intl.Collator take care of that for us?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

sortEntries(combinedEntries) {
Expand All @@ -354,7 +350,7 @@ class Directory {
return combinedEntries.sort((first, second) => {
const firstName = this.normalizeEntryName(first)
const secondName = this.normalizeEntryName(second)
return firstName.localeCompare(secondName)
return this.compareEntries(firstName, secondName)
})
}
}
Expand Down Expand Up @@ -461,3 +457,5 @@ class Directory {
return this.path === dirname
}
}

const compareFn = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}).compare
Loading