AeroSpace icon indicating copy to clipboard operation
AeroSpace copied to clipboard

Workspaces in menu bar are sorted by string rather than integer

Open nalderto opened this issue 1 year ago • 3 comments

AeroSpace v0.8.4-Beta 4951cbc3167737235c4dcf9c7521bd9a6418c699 macOS 14.3.1

image

nalderto avatar Feb 15 '24 22:02 nalderto

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.

rami3l avatar Feb 16 '24 02:02 rami3l

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

nikitabobko avatar Feb 16 '24 10:02 nikitabobko

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.

rami3l avatar Feb 19 '24 14:02 rami3l

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.CompareOptions Numbers within strings are compared using numeric value, that is, Name2.txt < Name7.txt < Name25.txt. https://developer.apple.com/documentation/foundation/nsstring/compareoptions#1594071

rami3l avatar Jun 09 '24 01:06 rami3l

Fixed in 0.12.0-Beta

nikitabobko avatar Jun 16 '24 16:06 nikitabobko