Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file are not sorted in ascending order for numeric file name #104555

Closed
rajeevbarde opened this issue Aug 13, 2020 · 17 comments
Closed

file are not sorted in ascending order for numeric file name #104555

rajeevbarde opened this issue Aug 13, 2020 · 17 comments
Assignees
Labels
feature-request Request for new features or functionality file-explorer Explorer widget issues *out-of-scope Posted issue is not in scope of VS Code
Milestone

Comments

@rajeevbarde
Copy link

  • VSCode Version: 1.47.3
  • OS Version: Windows 10 v2004

On VS Explorer, the file is not sorted in ascending order as shown below, (compared with Window Explorer)
image

Does this issue occur when all extensions are disabled?: Yes

@vscodebot
Copy link

vscodebot bot commented Aug 13, 2020

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@isidorn isidorn added feature-request Request for new features or functionality file-explorer Explorer widget issues labels Aug 13, 2020
@isidorn isidorn added this to the Backlog milestone Aug 13, 2020
@isidorn
Copy link
Contributor

isidorn commented Aug 13, 2020

We are fine tuning our sorting.
I believe we have not covered this case. Thus assigning to Backlog and we might tackle in the future.

fyi @leilapearson

@leilapearson
Copy link
Contributor

Thanks for the report @rajeevbarde

Yes, unfortunately numbers with a decimal separator will not always sort numerically. This is expected.

There are two things at play here:

  1. We're using a standard javascript localeCompare() with numeric sorting enabled, which essentially works by ignoring leading zeros when comparing number segments. That means that 1 < 02 < 003 (good) and 1.1 < 1.02 < 1.003 (maybe not good)

  2. We're disambiguating equivalent numbers (same number but different number of leading zeros) by length - and putting the shorter numbers first - so 1 < 01 < 001 and 1.1 < 1.01 < 1.001.

(1) is tricky. The reason I say "maybe not good" instead of just "not good" is that the decimal character and thousands separator characters differ by locale and also contextually. For example, a dot in a version number or in an IP address is not a decimal separator - it just separates number groups. I think this is why localeCompare() doesn't bother trying to account for decimal separators and thousands separators in different locales.

(2) would be easy enough to change. I could just reverse it so that longer equivalent numbers come first instead of last. I thought it was nicer to sort shorter equivalent numbers first - and more consistent with - for example - sorting a < aa < aaa. It is probably more common to sort them last though.

The simple change for (2) would resolve the specific cases in this report but if you expand the cases a bit you'll still see 2.01 < 2.1 < 2.02 < 2.2 - not the (possibly) desired 2.01 < 2.02 < 2.1 < 2.2 - so numbers with decimals would still not be in numerical order. Thus I'm not sure that a change would be worthwhile.

@ronakg
Copy link

ronakg commented Nov 13, 2020

@leilapearson I want to report another sorting issue here. Let me know if I should create a new bug for this.

For a case where 2 filenames have the same prefix and 1 of them doesn't have any suffix at all, the file without the suffix should be sorted ahead of the one with a suffix. A file named prefix.ext should be sorted ahead of prefixSuffix.ext or prefix_suffix.ext.

Take a look at the following example. close.go, close_test.go and close_channel.go are not sorted alphanumerically.

VSCode sorting -
Screen Shot 2020-11-13 at 11 39 31 AM

Bash sorting -
Screen Shot 2020-11-13 at 11 39 40 AM

@leilapearson
Copy link
Contributor

Hi @ronakg. The reason this happens is that vscode sorts in locale order but bash shorts in unicode order. Locale order sorts the underscore character before the dot character, but unicode order does the opposite. I submitted a PR that adds the option for vscode to sort in unicode order - as well as options to sort in locale order but group by uppercase first or lowercase first. That PR is currently on hold though as the maintainers evaluate the level of demand for these new settings.

See PR #97272 for more detail. Issue #99955 also provides a lot more background if you're interested.

@ronakg
Copy link

ronakg commented Nov 16, 2020

@leilapearson that option would be nice. Thanks for the info.

@fastrde
Copy link

fastrde commented Jan 9, 2021

This is what happened for me with Sort Order = default
Looks like the ordering is by the value of the first digits and not alphanumerical.

9658.. < 09765.. < 9778... < 12705... and so on

Version: 1.52.1
Commit: ea3859d
Date: 2020-12-16T16:30:02.420Z (3 wks ago)
Electron: 9.3.5
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Darwin x64 19.6.0

order

@leilapearson
Copy link
Contributor

@fastrde Hex numbers aren't going to sort so nicely unfortunately... The default sort will sort numerically - ignoring leading zeros - and considering the number to be complete once it reaches anything non-numeric... The PR I mentioned above would provide a unicode sorting option that might give you what you are looking for... not a nice hex sort, but at least a character by character comparison sort.

@fastrde
Copy link

fastrde commented Feb 3, 2021

@leilapearson I'm looking for what is described as the default sorting in VSCode. I don't want to sort hex numbers by value. I don't want to sort numbers by value either. I want to sort alphabetical. That's all :-). The description of sort order default is Files and folders are sorted by their names, in alphabetical order. Folders are displayed before files.
So I hope, that the mentioned PRs implements the default behavior. At the moment the description didn't match with the applied sorting..

default sorting at the moment

01apple    (01apple   = numerical value  1...)
11coconut  (11coconut = numerical value 11...)
012banana  (012banana = numerical value 12...)

Sorting I expected when I read the description

012banana  (012banana = alphabetic 012...)
01apple    (01apple   = alphabetic 01a...)
11coconut  (11coconut = alphabetic 11c...)

@leilapearson
Copy link
Contributor

@fastrde that makes sense. Unfortunately the text is misleading. The actual behavior - since way before I got involved - has been to do a localeCompare with the numeric option enabled. So no, it isn't a truly an alphabetical search. I guess someone could fix the text to better describe the actual behavior since it is somewhat misleading.

I did submit an earlier pull request that provided the ability to do the various sort options with the numeric option turned off, but it didn't seem like many people were asking for that option and the maintainers requested a less complicated PR so I withdrew it.

If enough people want a locale-based alphabetical comparison that doesn't have numeric sorting enabled - and the maintainers agree - then that feature could certainly be added at a later date.

@leilapearson
Copy link
Contributor

Also, I'm happy to submit a new PR to add that new "non-numeric" option if the maintainers give the go ahead.

@paolieri
Copy link

paolieri commented Mar 6, 2021

A "non-numeric" option would be very useful; I was bitten by this too, in the same scenario as @fastrde (a directory with many files containing hex hashes in their name).

@isidorn isidorn added the *out-of-scope Posted issue is not in scope of VS Code label May 4, 2021
@paolieri
Copy link

paolieri commented May 4, 2021

@leilapearson was waiting for the go-ahead to submit a PR adding a "non-numeric" file sort option.
This would have the benefits of:

  • preserving the current/default behavior (to avoid surprises)
  • giving an option for those who need files in true alphabetical order (as ls would show them)

@isidorn I hope maintainers can reconsider this, I believe it is in the scope of VSCode

@isidorn
Copy link
Contributor

isidorn commented May 5, 2021

I am discussing a potential PR with @leilapearson on another issue, once we tackle that we might look into this.

@Leedehai
Copy link

Hi @isidorn :) Wondering if there is a decision on the PR you mentioned above? Could you re-open this issue?

@leilapearson
Copy link
Contributor

@Leedehai the PR he was referring to was merged earlier this month and is currently available in VS Code Insiders builds and should be in the next VS Code release which I think is due out pretty soon. It doesn't directly address this issue, but there is a unicode option which is a non-numeric sort which will perhaps meet your needs? If you have a chance to try it, let us know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality file-explorer Explorer widget issues *out-of-scope Posted issue is not in scope of VS Code
Projects
None yet
Development

No branches or pull requests

8 participants
@leilapearson @ronakg @paolieri @fastrde @isidorn @rajeevbarde @Leedehai and others