Workspaces in menu bar are sorted by string rather than integer
AeroSpace v0.8.4-Beta 4951cbc3167737235c4dcf9c7521bd9a6418c699 macOS 14.3.1
I'm looking at:
https://github.com/nikitabobko/AeroSpace/blob/0ce757b970a3cfd95a420eeba0bfe3b38a2f3609/src/tree/Workspace.swift#L40-L42
If it's just a problem of sorting items for displaying purposes, I suggest using localizedStandardCompare as described in https://stackoverflow.com/a/43870210.
Workspace is an arbitrary string. It's not necessarily a number
Your alternative is to rename workspaces to 01, 02, ..., 10
I suggest using localizedStandardCompare as described in https://stackoverflow.com/a/43870210.
https://developer.apple.com/documentation/foundation/nsstring/1409742-localizedstandardcompare
This method should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact sorting behavior of this method is different under different locales and may be changed in future releases. This method uses the current locale.
I'm not sure that I want to use localizedStandardCompare. It sounds too magical. The semantics of the API is not clear
Your alternative is to rename workspaces to
01,02, ...,10
I've thought about that workaround and got a significantly wider tray icon for just fixing the order, and I don't think it's worth it.
Actually I think a better workaround would be removing 10.
I'm not sure that I want to use
localizedStandardCompare. It sounds too magical. The semantics of the API is not clear
You're right, that's why I only recommend to use it for UI purposes (and it works pretty well actually). For other purposes, I guess you won't need to change the current sorting mechanism if it works well for you.
I'm not sure that I want to use
localizedStandardCompare. It sounds too magical. The semantics of the API is not clear
@nikitabobko I just did a bit more research, and the source code is here:
public func localizedStandardCompare(_ string: String) -> ComparisonResult {
return compare(string, options: [.caseInsensitive, .numeric, .widthInsensitive, .forcedOrdering], range: NSRange(location: 0, length: length), locale: Locale.current._bridgeToObjectiveC())
}
There are indeed way too many options involved, and most of them are unrelated to our issue here.
However I think .numeric does what I'd expect here, which should be way more predictable:
static var numeric: NSString.CompareOptionsNumbers within strings are compared using numeric value, that is, Name2.txt < Name7.txt < Name25.txt. https://developer.apple.com/documentation/foundation/nsstring/compareoptions#1594071
Fixed in 0.12.0-Beta