0

I have data like below. I need to compare the numbers in each field and rank the numbers by date and versions. I have tried explode and split but only the first field (10, 11, 10) are returning.

2018-07-01 10.1.1
2018-07-01 11.1.1
2018-08-02 10.0.5
Hua Cha
  • 107
  • 1
  • 9
  • Did you look at https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java – tk421 Oct 02 '18 at 20:15
  • A straightforward implementation is comparing the first major version, if they're equal then compare the next minors until you either reach a resolution or total equality. – mohkamfer Oct 02 '18 at 19:36
  • How would I go about comparing each field? The versions are stored as strings. – Hua Cha Oct 02 '18 at 19:46
  • Well... I don't really know which programming language you're using, but basically the two concepts are exploding/splitting strings and parsing strings as integers. – mohkamfer Oct 02 '18 at 20:15
  • Sorry. I'm doing this in Hive. – Hua Cha Oct 02 '18 at 20:18
  • While the general approach is true, I'm not sure this answer directly addresses the problem of the OP. – KevinO Oct 03 '18 at 00:22

1 Answers1

0

split(version,'\\.') will return array of version numbers:

Major version number is split(version,'\\.')[0]

Minor version is split(version,'\\.')[1]

And the third number is split(version,'\\.')[2]

Use cast(string as int) to convert them to int, like this: cast(split(version,'\\.')[0] as int)

Compare them one by one.

leftjoin
  • 36,950
  • 8
  • 57
  • 116
  • So the versions are in string format right now. Do I convert the string to int and then do split? Or split and then cast to int? – Hua Cha Oct 03 '18 at 04:47
  • @HuaCha Yes, they are strings. Use `cast(split(version,'\\.')[0] as int)` to convert to int – leftjoin Oct 03 '18 at 04:49