0

I'm writing a VC++ program that looks for all the installed versions of java in a system (like a single system having java 1.7, 1.8 etc). I want to compare the versions and display them in descending (or ascending order).
For instance, it should print:
Version:1.8.0_60 is the highest version.
Version:1.7.0_80 is the second highest version.
.
.
in that order. My code lists different versions of installed JREs. No problem in that. But as you may have guessed, I have no idea on how to compare the versions returned and find which version is highest ,which version is second highest etc. Converting them into integer or float and comparing wont work obviously since the listed versions also have '_'(underscore) character present in them. So, my guess is I will have to compare them as they are and sort them in ascending or descending order. How do I compare these TCHAR values and sort them?

pikaaaaaaaaa
  • 95
  • 12

1 Answers1

0

You can convert your version string int four-part numeric value like that:

TCHAR* s1 = _T("1.8.0_60");
int i1, i2, i3, i4;
_stscanf_s(s1, _T("%d.%d.%d_%d"), &i1, &i2, &i3, &i4);

If you then assume that neither part is going to be greater than 255, you can combine them into one 32-bit value for easy comparison while sorting.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27