fix: wrong name sorting in case of folders with different length #898
Massimo Melina committed
Feb 15, 2025 at 16:02 UTC
97a04199d87e8c3e2d04cef21df1742199a5f2ba
2 files changed
+14
-11
frontend/src/state.ts
+1
-1
@@ -127,7 +127,7 @@ export class DirEntry implements ServerDirEntry {
127
this.t = this.m || this.c
128
this.name = this.isFolder ? this.n.slice(this.n.lastIndexOf('/', this.n.length - 2) + 1, -1)
129
: this.n.slice(this.n.lastIndexOf('/') + 1)
130
- const x = this.isFolder && !this.web ? 'L' : 'R' // to open we need list for folders and read for files
130
+ const x = this.isFolder && !this.web ? 'L' : 'R' // to open we need list for folders and read for files
131
this.cantOpen = this.p?.match(x) ? true : this.p?.match(x.toLowerCase()) ? DirEntry.FORBIDDEN : undefined
132
}
133
frontend/src/useFetchList.ts
+13
-10
@@ -178,20 +178,24 @@ function sort(list: DirList) {
178
const byTime = sort_by === 'time'
179
const byCreation = sort_by === 'creation'
180
const invert = state.invert_order ? -1 : 1
181
- return list.sort((a,b) =>
182
- -compare(a.order||0, b.order||0)
181
+ return list.sort((a, b) =>
182
+ -compareScalar(a.order||0, b.order||0)
183
|| hfsEvent('sortCompare', { a, b }).find(Boolean)
184
- || folders_first && -compare(a.isFolder, b.isFolder)
185
- || invert * (bySize ? compare(a.s||0, b.s||0)
184
+ || folders_first && -compareScalar(a.isFolder, b.isFolder)
185
+ || invert * (bySize ? compareScalar(a.s||0, b.s||0)
186
: byExt ? localCompare(a.ext, b.ext)
187
- : byTime ? compare(a.t, b.t)
188
- : byCreation ? compare(a.c, b.c)
187
+ : byTime ? compareScalar(a.t, b.t)
188
+ : byCreation ? compareScalar(a.c, b.c)
189
: 0
190
)
191
|| sort_numerics && (invert * compareNumerics(a.n, b.n))
192
- || invert * localCompare(a.n, b.n) // fallback to name/path
192
+ || invert * localCompare(nameToCompare(a), nameToCompare(b)) // fallback to name/path
193
)
194
195
+ function nameToCompare(x: DirEntry) { // try to avoid slicing. When searching, we need to consider the path
196
+ return !x.isFolder ? x.n : !state.remoteSearch ? x.name : x.n.slice(0, -1)
197
+ }
198
+
199
function compareNumerics(a: string, b: string) {
200
const re = /\d/g
201
if (!re.exec(a)) return 0
@@ -201,12 +205,11 @@ function sort(list: DirList) {
205
a = a.slice(i-1)
206
b = b.slice(i-1)
207
}
204
- return compare(parseFloat(a), parseFloat(b))
208
+ return compareScalar(parseFloat(a), parseFloat(b))
209
}
210
}
211
208
-// generic comparison
209
-function compare(a:any, b:any) {
212
+function compareScalar(a:any, b:any) {
213
return a - b
214
}
215