numeric-names option will now work also with if the numbers are not at the start of the name
Massimo Melina committed
Sep 24, 2024 at 16:22 UTC
a0c0906d3be1427b7a3e5348505dcc0d359451ee
1 file changed
+14
-2
frontend/src/useFetchList.ts
+14
-2
@@ -7,7 +7,7 @@ import _ from 'lodash'
7
import { subscribeKey } from 'valtio/utils'
8
import { useIsMounted } from 'usehooks-ts'
9
import { alertDialog } from './dialog'
10
-import { hfsEvent, HTTP_MESSAGES, HTTP_METHOD_NOT_ALLOWED, HTTP_UNAUTHORIZED, LIST, urlParams, waitFor, xlate } from './misc'
10
+import { hfsEvent, HTTP_MESSAGES, HTTP_METHOD_NOT_ALLOWED, HTTP_UNAUTHORIZED, LIST, urlParams, xlate } from './misc'
11
import { t } from './i18n'
12
import { useLocation, useNavigate } from 'react-router-dom'
13
import { closeLoginDialog } from './login'
@@ -152,9 +152,21 @@ function sort(list: DirList) {
152
: byTime ? compare(a.t, b.t)
153
: 0
154
)
155
- || sort_numerics && (invert * compare(parseFloat(a.n), parseFloat(b.n)))
155
+ || sort_numerics && (invert * compareNumerics(a.n, b.n))
156
|| invert * localCompare(a.n, b.n) // fallback to name/path
157
)
158
+
159
+ function compareNumerics(a: string, b: string) {
160
+ const re = /\d/g
161
+ if (!re.exec(a)) return 0
162
+ const i = re.lastIndex
163
+ if (i) { // doesn't start with a number
164
+ if (!b.startsWith(a.slice(0, i -1))) return 0 // b is comparable only if it has same leading part
165
+ a = a.slice(i-1)
166
+ b = b.slice(i-1)
167
+ }
168
+ return compare(parseFloat(a), parseFloat(b))
169
+ }
170
}
171
172
// generic comparison