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

List Rubies in natural version sort order. #628

Closed
wants to merge 2 commits into from
Closed

List Rubies in natural version sort order. #628

wants to merge 2 commits into from

Conversation

havenwood
Copy link
Contributor

Show Rubies listed by ruby-build --definitions in a nice sort order by Ruby names then versions. This commit uses a combination of sed, sort and cut to achieve the desired result since GNU ls and sort natural version number sorting options aren't portable.

First normalize each Ruby (by converting dashes and plusses to periods) followed by a tab then the original Ruby version:

sed -e "h" -e 's/[+-]/./g' -e "G" -e 's/\n/ /'

For example, jruby-9000+graal-dev becomes jruby.9000.graal.dev\tjruby-9000+graal-dev.

Then dictionary sort the first column and numerically sort the next four columns:

sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" -k "5,5n"

For most non-MRI Rubies, this results in the Ruby name being dictionary sorted and the MAJOR, MINOR, TINY, plus one more column being numerically sorted. Additional numeric sort columns could be added but aren't needed at present. For all existing ruby-build Ruby versions this sort order is correct.

Once natural version number sorted, cut to the original Ruby versions column with a cut -f 2.

Closes #512.

One caveat is that with ruby-build's special case dropping the CRuby ruby- prefix (see #543), a future MRI version 10.0+ could be missorted. I'm assuming 1.X will be long dead before a 10.0+ comes around but now we've had a JRuby 9000 so who knows.

See chruby #278 and #277 for more discussion of related numeric sorting of Ruby versions with sed.

First normalize each Ruby (by converting dashes and plusses to periods) followed by a tab then the original Ruby version:
```shell
sed -e "h" -e 's/[+-]/./g' -e "G" -e 's/\n/	/'
```
For example, `jruby-9000+graal-dev` becomes `jruby.9000.graal.dev\tjruby-9000+graal-dev`.

Then dictionary sort the first column and numerically sort the next four columns:
```shell
sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" -k "5,5n"
```
For most non-MRI Rubies, this results in the Ruby name being dictionary sorted and the MAJOR, MINOR, TINY, plus one more column being numerically sorted. Additional numeric sort columns could be added but aren't needed at present. For all existing ruby-build Ruby versions this sort order is correct.

Once lexicographically sorted, cut to the original Ruby versions column with a `cut -f 2`.

Closes #512.

One caveat is that with ruby-build's special case dropping the CRuby `ruby-` prefix (see #543), a future MRI version 10.0+ could miss-sorted. I'm assuming 1.X will be long dead before a 10.0+ comes around but now we've had a JRuby 9000 so who knows.

See chruby [#278](postmodern/chruby#278) and [#277](postmodern/chruby#277) for more discussion of related numeric sorting of Ruby versions with sed.
@havenwood
Copy link
Contributor Author

Previous sort order:

1.8.6-p383
1.8.6-p420
1.8.7-p249
1.8.7-p302
1.8.7-p334
1.8.7-p352
1.8.7-p357
1.8.7-p358
1.8.7-p370
1.8.7-p371
1.8.7-p374
1.8.7-p375
1.9.1-p378
1.9.1-p430
1.9.2-p0
1.9.2-p180
1.9.2-p290
1.9.2-p318
1.9.2-p320
1.9.2-p326
1.9.2-p330
1.9.3-dev
1.9.3-p0
1.9.3-p125
1.9.3-p194
1.9.3-p286
1.9.3-p327
1.9.3-p362
1.9.3-p374
1.9.3-p385
1.9.3-p392
1.9.3-p429
1.9.3-p448
1.9.3-p484
1.9.3-p545
1.9.3-p547
1.9.3-preview1
1.9.3-rc1
2.0.0-dev
2.0.0-p0
2.0.0-p195
2.0.0-p247
2.0.0-p353
2.0.0-p451
2.0.0-p481
2.0.0-preview1
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.1.0
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.1.1
2.1.2
2.2.0-dev
jruby-1.5.6
jruby-1.6.3
jruby-1.6.4
jruby-1.6.5
jruby-1.6.5.1
jruby-1.6.6
jruby-1.6.7
jruby-1.6.7.2
jruby-1.6.8
jruby-1.7.0
jruby-1.7.0-preview1
jruby-1.7.0-preview2
jruby-1.7.0-rc1
jruby-1.7.0-rc2
jruby-1.7.1
jruby-1.7.10
jruby-1.7.11
jruby-1.7.12
jruby-1.7.13
jruby-1.7.14
jruby-1.7.15
jruby-1.7.2
jruby-1.7.3
jruby-1.7.4
jruby-1.7.5
jruby-1.7.6
jruby-1.7.7
jruby-1.7.8
jruby-1.7.9
jruby-9000+graal-dev
jruby-9000-dev
maglev-1.0.0
maglev-1.1.0-dev
maglev-2.0.0-dev
mruby-1.0.0
mruby-dev
rbx-1.2.4
rbx-2.0.0
rbx-2.0.0-dev
rbx-2.0.0-rc1
rbx-2.1.0
rbx-2.1.1
rbx-2.2.0
rbx-2.2.1
rbx-2.2.10
rbx-2.2.2
rbx-2.2.3
rbx-2.2.4
rbx-2.2.5
rbx-2.2.6
rbx-2.2.7
rbx-2.2.9
ree-1.8.6-2009.06
ree-1.8.7-2009.09
ree-1.8.7-2009.10
ree-1.8.7-2010.01
ree-1.8.7-2010.02
ree-1.8.7-2011.03
ree-1.8.7-2011.12
ree-1.8.7-2012.01
ree-1.8.7-2012.02
topaz-dev

Pull request sort order:

1.8.6-p383
1.8.6-p420
1.8.7-p249
1.8.7-p302
1.8.7-p334
1.8.7-p352
1.8.7-p357
1.8.7-p358
1.8.7-p370
1.8.7-p371
1.8.7-p374
1.8.7-p375
1.9.1-p378
1.9.1-p430
1.9.2-p0
1.9.2-p180
1.9.2-p290
1.9.2-p318
1.9.2-p320
1.9.2-p326
1.9.2-p330
1.9.3-dev
1.9.3-p0
1.9.3-p125
1.9.3-p194
1.9.3-p286
1.9.3-p327
1.9.3-p362
1.9.3-p374
1.9.3-p385
1.9.3-p392
1.9.3-p429
1.9.3-p448
1.9.3-p484
1.9.3-p545
1.9.3-p547
1.9.3-preview1
1.9.3-rc1
2.0.0-dev
2.0.0-p0
2.0.0-p195
2.0.0-p247
2.0.0-p353
2.0.0-p451
2.0.0-p481
2.0.0-preview1
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.1.0
2.1.1
2.1.2
2.2.0-dev
jruby-1.5.6
jruby-1.6.3
jruby-1.6.4
jruby-1.6.5.1
jruby-1.6.5
jruby-1.6.6
jruby-1.6.7.2
jruby-1.6.7
jruby-1.6.8
jruby-1.7.0-preview1
jruby-1.7.0-preview2
jruby-1.7.0-rc1
jruby-1.7.0-rc2
jruby-1.7.0
jruby-1.7.1
jruby-1.7.2
jruby-1.7.3
jruby-1.7.4
jruby-1.7.5
jruby-1.7.6
jruby-1.7.7
jruby-1.7.8
jruby-1.7.9
jruby-1.7.10
jruby-1.7.11
jruby-1.7.12
jruby-1.7.13
jruby-1.7.14
jruby-1.7.15
jruby-9000-dev
jruby-9000+graal-dev
maglev-1.0.0
maglev-1.1.0-dev
maglev-2.0.0-dev
mruby-dev
mruby-1.0.0
rbx-1.2.4
rbx-2.0.0
rbx-2.0.0-dev
rbx-2.0.0-rc1
rbx-2.1.0
rbx-2.1.1
rbx-2.2.0
rbx-2.2.1
rbx-2.2.2
rbx-2.2.3
rbx-2.2.4
rbx-2.2.5
rbx-2.2.6
rbx-2.2.7
rbx-2.2.9
rbx-2.2.10
ree-1.8.6-2009.06
ree-1.8.7-2009.09
ree-1.8.7-2009.10
ree-1.8.7-2010.01
ree-1.8.7-2010.02
ree-1.8.7-2011.03
ree-1.8.7-2011.12
ree-1.8.7-2012.01
ree-1.8.7-2012.02
topaz-dev

@nirvdrum
Copy link
Contributor

nirvdrum commented Sep 7, 2014

The PR comment here is very helpful in explaining what you're doing, but I think the commit comment is misleading since you're doing more than sorting lexicographically.

@havenwood havenwood changed the title Sort Ruby versions lexicographically. Natural sort Ruby versions. Sep 7, 2014
@havenwood havenwood changed the title Natural sort Ruby versions. List Rubies in natural version sort order. Sep 7, 2014
@havenwood
Copy link
Contributor Author

@nirvdrum I didn't mean to be misleading. Does "List Rubies in natural version sort order." seem better or if I'm missing the point could you suggest something appropriate?

Also re-scanning the order I noticed this remaining issue:

jruby-1.6.5.1
jruby-1.6.5

and

jruby-1.6.7.2
jruby-1.6.7

@havenwood
Copy link
Contributor Author

My intention is equivalent sort order to GNU's ls -v, natural sort of (version) numbers within text or GNU's sort -V, natural sort of (version) numbers within text.

Edit: With the exception of the + substitution. I don't know if the naming convention of jruby-9000+graal-dev is going to be continued but I tried to accommodate it.

What I thought was an abundance of caution with the extra sort column ended up causing the four-digit JRuby versions to be missorted. The fall-through sorting appears to sort properly for columns beyond the four that are explicitly sorted.
@havenwood
Copy link
Contributor Author

Updated (fixed) sort order:

1.8.6-p383
1.8.6-p420
1.8.7-p249
1.8.7-p302
1.8.7-p334
1.8.7-p352
1.8.7-p357
1.8.7-p358
1.8.7-p370
1.8.7-p371
1.8.7-p374
1.8.7-p375
1.9.1-p378
1.9.1-p430
1.9.2-p0
1.9.2-p180
1.9.2-p290
1.9.2-p318
1.9.2-p320
1.9.2-p326
1.9.2-p330
1.9.3-dev
1.9.3-p0
1.9.3-p125
1.9.3-p194
1.9.3-p286
1.9.3-p327
1.9.3-p362
1.9.3-p374
1.9.3-p385
1.9.3-p392
1.9.3-p429
1.9.3-p448
1.9.3-p484
1.9.3-p545
1.9.3-p547
1.9.3-preview1
1.9.3-rc1
2.0.0-dev
2.0.0-p0
2.0.0-p195
2.0.0-p247
2.0.0-p353
2.0.0-p451
2.0.0-p481
2.0.0-preview1
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.1.0
2.1.1
2.1.2
2.2.0-dev
jruby-1.5.6
jruby-1.6.3
jruby-1.6.4
jruby-1.6.5
jruby-1.6.5.1
jruby-1.6.6
jruby-1.6.7
jruby-1.6.7.2
jruby-1.6.8
jruby-1.7.0
jruby-1.7.0-preview1
jruby-1.7.0-preview2
jruby-1.7.0-rc1
jruby-1.7.0-rc2
jruby-1.7.1
jruby-1.7.2
jruby-1.7.3
jruby-1.7.4
jruby-1.7.5
jruby-1.7.6
jruby-1.7.7
jruby-1.7.8
jruby-1.7.9
jruby-1.7.10
jruby-1.7.11
jruby-1.7.12
jruby-1.7.13
jruby-1.7.14
jruby-1.7.15
jruby-9000-dev
jruby-9000+graal-dev
maglev-1.0.0
maglev-1.1.0-dev
maglev-2.0.0-dev
mruby-dev
mruby-1.0.0
rbx-1.2.4
rbx-2.0.0
rbx-2.0.0-dev
rbx-2.0.0-rc1
rbx-2.1.0
rbx-2.1.1
rbx-2.2.0
rbx-2.2.1
rbx-2.2.2
rbx-2.2.3
rbx-2.2.4
rbx-2.2.5
rbx-2.2.6
rbx-2.2.7
rbx-2.2.9
rbx-2.2.10
ree-1.8.6-2009.06
ree-1.8.7-2009.09
ree-1.8.7-2009.10
ree-1.8.7-2010.01
ree-1.8.7-2010.02
ree-1.8.7-2011.03
ree-1.8.7-2011.12
ree-1.8.7-2012.01
ree-1.8.7-2012.02
topaz-dev

@nirvdrum
Copy link
Contributor

nirvdrum commented Sep 7, 2014

@havenwood That does sound better. I was confused when I first saw the PR because the previous ordering was lexicographic, so I wasn't sure what changed. Otherwise, I like the changes.

@@ -870,7 +870,9 @@ list_definitions() {
{ for definition in "${RUBY_BUILD_ROOT}/share/ruby-build/"*; do
echo "${definition##*/}"
done
} | sort
} | sed -e "h" -e 's/[+-]/./g' -e "G" -e 's/\n/ /' |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The literal ctrl-v tab in 's/\n/ /' is for portability since OS X's sed doesn't properly support \t tabs.

@mislav
Copy link
Member

mislav commented Sep 8, 2014

1.9.3-p547
1.9.3-preview1
1.9.3-rc1

I don't like that even with this PR, pre-release versions ("dev", "preview", "rc") are sorted after stable releases. I've taken a stab at this myself in #631

@mislav mislav closed this in #631 Sep 8, 2014
@havenwood
Copy link
Contributor Author

I didn't think of pre-release order, but yeah that's nice.

@havenwood havenwood deleted the lexicographic-ruby-sort branch September 8, 2014 22:16
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

Successfully merging this pull request may close these issues.

3 participants