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

sort files in UI in natural order rather than alphabetically #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/app/home/helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,21 @@ export class HelperService {
return adjusted;
}

/**
* Natural sort a list of strings, mutates array and return sorted array
* @param array
*/
public natural_sort(array) {
// using `localeCompare()` rather than implementing from scratch
// https://stackoverflow.com/questions/2802341/javascript-natural-sort-of-alphanumerical-strings
// https://fuzzytolerance.info/blog/2019/07/19/The-better-way-to-do-natural-sort-in-JavaScript/
array.sort((a, b) =>
a.localeCompare(b, navigator.languages[0] || navigator.language, {
numeric: true,
ignorePunctuation: true,
})
);
return array;
}

}
4 changes: 2 additions & 2 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class HomeComponent implements AfterViewInit, OnInit {
this.electronService.ipcRenderer.send('just-started');

this.electronService.ipcRenderer.on('file-chosen', (event, filePath: string[]) => {
this.addToFileList(filePath.sort()); // sort alphabetically
this.addToFileList(this.helperService.natural_sort(filePath));
});

this.electronService.ipcRenderer.on('txt-file-updated', (event, newText: string) => {
Expand Down Expand Up @@ -192,7 +192,7 @@ export class HomeComponent implements AfterViewInit, OnInit {
}

if (this.mode === 'edit') {
this.addToFileList(fileList.sort()); // sort alphabetically
this.addToFileList(this.helperService.natural_sort(fileList));
}
}
}
Expand Down