Skip to content

In Groovy, null is less than everything

August 12, 2013

I stumbled upon this fact today: In Groovy, null is less than everything except itself. Below I’ve attached a Gist that has some sample code and the output of this code running Groovy 2.1.6.

A good answer to why this behavior should exist is that when sorting collections with ‘null’ entries, the null entries will all be grouped together at the beginning if using natural ordering.

However for comparison, the Ruby programming language does NOT allow this comparison or this sorting behavior. Comparing anything with nil in Ruby results in an Error. This is just another Groovy gotcha to lookout for in addition to these: pitfalls and differences from Java that might be surprising to Java developers.

Advertisements

From → Groovy

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: