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

['1.100', '1.10', '1.1', '1.54'] etc do not sort properly... #13

Open
rubenstolk opened this issue Sep 23, 2013 · 7 comments
Open

['1.100', '1.10', '1.1', '1.54'] etc do not sort properly... #13

rubenstolk opened this issue Sep 23, 2013 · 7 comments

Comments

@rubenstolk
Copy link

No description provided.

overset pushed a commit that referenced this issue Apr 22, 2015
@overset
Copy link
Owner

overset commented May 4, 2015

Elements like 1.100 are implicitly coerced into floats so ['1.100', '1.10', '1.1'] are each interpreted as 1.1 when performing the sort comparison. In every browser but Chrome this will be "stable" e.g. they will remain in the same order prior to calling .sort(). I'm working through a simplified version that does less of the coercion and relies on the data type of the elements in the array supplied thus ['1.100', '1.10', '1.1'] would sort to a more "natural" ['1.1', '1.10', '1.100'].

@harisb
Copy link

harisb commented Jan 26, 2016

Hey @overset, did you ever come up with a solution for this?

I also have same problem where for example ['2.2 sec','1.9 sec','1.53 sec'] will sort as [1.9 sec, 1.53 sec, 2.2 sec]

overset pushed a commit that referenced this issue Jan 29, 2016
+ added better unicode locale support fixing #9
+ added regex lookahead for better chunking with mixed number/strings
+ fix for #13 (comment)
@overset
Copy link
Owner

overset commented Jan 29, 2016

@harisb - I posted a fix for your example in v0.8.1

>>> ['2.2 sec','1.9 sec','1.53 sec'].sort(naturalSort)
["1.53 sec", "1.9 sec", "2.2 sec"]

I kept the float inference for strings that resemble numbers, e.g. ['1.100'] will still be coerced to 1.1. I'm still thinking it would be a good idea to rely on the input type instead of forcing coercion, e.g. if you send ['1.100'] it will chunk the string into 2 separate integers instead of force it to be a float. So we can then handle cases like:

>>> ['1.100','1.1','1.10'].sort(naturalSort)
["1.1", "1.10", "1.100"]
>>> [1.100, 1.1, 1.10].sort(naturalSort)
[1.1, 1.1, 1.1]

An original design feature was to coerce as many string values into their native data type as possible but this could be simplified and made more consistent.

@harisb
Copy link

harisb commented Jan 29, 2016

Awesome, that works! Thank you!

@sbichenko
Copy link

Should this issue be closed?

@macronx
Copy link

macronx commented Jul 11, 2016

The fix for '2.2sec' breaks version numbers like '1.1a'.

Compare:

['1.1a', '1.1b', '1.10a'].sort(naturalSort)
['v1.1a', 'v1.1b', 'v1.10a'].sort(naturalSort)

The problem is determining the difference between a floating point number and a two-level version number. I think making floats be a 'word' on their own may work more intuitively. That way, '2.2 sec' would sort as expected, but things like '2.2sec' and '1.1a' would still sort as version numbers (even though to a human, the former is obviously not). My two cents.

@linquize
Copy link

3eabc73 breaks the unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants