naturally icon indicating copy to clipboard operation
naturally copied to clipboard

Doesn't sort when normalize would produce arrays of different length

Open jeropaul opened this issue 9 years ago • 3 comments

Works well with structured data that is similar, but the following arbitrary strings cause a failure:

For example arr = [ "99-101 some street address", "Some other thing", "1"] Naturally.sort(arr)

results in ArgumentError: comparison of Array with Array failed, In this case ruby doesn't consider the arrays produced by Naturally.normalize to comparable.

jeropaul avatar Jul 08 '15 06:07 jeropaul

I can imagine that it fails. The thing is, though, what would the sort order be for those strings?

On Tue, Jul 7, 2015 at 11:45 PM, Jerome [email protected] wrote:

Works well with structured data that is similar, but the following arbitrary strings cause a failure:

For example arr = [ "99-101 some street address", "Some other thing", "1"] Naturally.sort(arr)

results in ArgumentError: comparison of Array with Array failed, In this case ruby doesn't consider the arrays produced by Naturally.normalize to comparable.

— Reply to this email directly or view it on GitHub https://github.com/dogweather/naturally/issues/21.

dogweather avatar Jul 08 '15 09:07 dogweather

I don't have a solid opinion in regards to what I would expect. At a pinch I'd be happy with:

  • "1", "99-101 some street address", "Some other thing"

but I'm assuming numbers before letters and I can see others having (valid) opinions that it should be the other way around. The main point is that it shouldn't stack trace out when sorting strings regardless of whether the normalize method tokenizes the strings differently.

jeropaul avatar Jul 10 '15 01:07 jeropaul

The main point is that it shouldn't stack trace out...

Ah hah, I agree with you there. I think better behavior would be for #sort to raise a more meaningful "Can't sort this" exception. And then we could have a new method like #sort_always which would never raise an exception. Instead it'd do a simple Array#sort.

dogweather avatar Jul 10 '15 02:07 dogweather