Skip to content

Commit

Permalink
Sort sub-locations alpha-numerically
Browse files Browse the repository at this point in the history
  • Loading branch information
RvanderLaan committed Nov 23, 2021
1 parent 08e066a commit 1319a40
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/entities/Location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface ISubLocation {
subLocations: ISubLocation[];
}

/** Sorts alphanumerically, "natural" sort */
const sort = (a: ISubLocation, b: ISubLocation) =>
a.name.localeCompare(b.name, undefined, { numeric: true });

export class ClientSubLocation implements ISubLocation {
@observable
name: string;
Expand All @@ -39,16 +43,18 @@ export class ClientSubLocation implements ISubLocation {
this.name = name;
this.isExcluded = excluded;
this.subLocations.push(
...subLocations.map(
(subLoc) =>
new ClientSubLocation(
this.location,
SysPath.join(path, subLoc.name),
subLoc.name,
subLoc.isExcluded,
subLoc.subLocations,
),
),
...subLocations
.sort(sort)
.map(
(subLoc) =>
new ClientSubLocation(
this.location,
SysPath.join(path, subLoc.name),
subLoc.name,
subLoc.isExcluded,
subLoc.subLocations,
),
),
);

makeObservable(this);
Expand Down Expand Up @@ -110,16 +116,18 @@ export class ClientLocation implements ISerializable<ILocation> {
this.extensions = extensions;

this.subLocations.push(
...subLocations.map(
(subLoc) =>
new ClientSubLocation(
this,
SysPath.join(this.path, subLoc.name),
subLoc.name,
subLoc.isExcluded,
subLoc.subLocations,
),
),
...subLocations
.sort(sort)
.map(
(subLoc) =>
new ClientSubLocation(
this,
SysPath.join(this.path, subLoc.name),
subLoc.name,
subLoc.isExcluded,
subLoc.subLocations,
),
),
);

makeObservable(this);
Expand Down Expand Up @@ -251,7 +259,7 @@ export class ClientLocation implements ISerializable<ILocation> {
subLoc.subLocations.replace([]);
}
}
loc.subLocations.replace(newSublocations);
loc.subLocations.replace(newSublocations.sort(sort));

this.isRefreshing = false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ const LocationCreationDialog = ({ location, onClose }: LocationCreationDialogPro
<legend>Included Subdirectories of {location.name}</legend>
{!sublocationsLoaded ? (
<i>{IconSet.LOADING} loading...</i>
) : location.subLocations.length === 0 ? (
<p>No subdirectories found.</p>
) : (
<SubLocationInclusionTree location={location} />
)}
Expand Down

0 comments on commit 1319a40

Please sign in to comment.