File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting version numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting version numbers" Watch "Sorting version numbers" New topic
Author

Sorting version numbers

Amitkumar Fulambarkar
Greenhorn

Joined: Sep 17, 2008
Posts: 29
Hi,

Can someone please advise how to sort the version numbers ascending or descending orders. Please provide some sample code snippet.

3.10.01
3.2.01
3.1.03
3.1.02
3.1.01


dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
What have you tried? The solution is going to require some custom String comparison that considers the elements in the version numbers individually.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
We don’t supply that sort of answer.
You need to work out on paper how to sort those version numbers. Is 3.2 before 3.10 or after? That can be very significant.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

You want to look at implementing your own Comparator<String> (assuming those are held as Strings in the first place), containing whatever logic should be applied. So all you need to do is specify how two version numbers should be compared, and the standard sort routines will do the rest.
Amitkumar Fulambarkar
Greenhorn

Joined: Sep 17, 2008
Posts: 29
I have to sort the list of the object and build versions are once of the properties of that object. I tried using Comparable interface. The object is implementing Comparable interface. I written the logic in compareTo() method using after(), before method, but for other properties of that object. However for some reason I cannot use it anymore and I have to sort it on the version numbers only. I am not sure how to use before and after for the versions numbers.

I am calling Collection.sort(list) which sorts the list.

I am not expecting ready made code that I will cut and paste but some clue.

The version list I provided is in descending order.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Amitkumar Fulambarkar wrote:I tried using Comparable interface. The object is implementing Comparable interface. I written the logic in compareTo() method using after(), before method, but for other properties of that object. However for some reason I cannot use it anymore and I have to sort it on the version numbers only. I am not sure how to use before and after for the versions numbers.


That doesn't really tell us anything about what you tried or what specific problems you had. It also doesn't answer the question Campbell asked about your specific rules for sorting.

1. You have to specify clear, precise, complete rules for how to sort, without any regard to Java. You'll need to do this both for yourself, to make sure you understand what your code is supposed to do, and for people here, if we're to be able to help you.

2. Once you clearly understand what your rules are, you write the code and test it.

3. If you can't get it to work, you need to provide your rules, your code (preferably in SSCCE form), your input, your expected output, and your actual output. In other words, you need to TellTheDetails.(⇐click)

Amitkumar Fulambarkar
Greenhorn

Joined: Sep 17, 2008
Posts: 29
I am iterating the list with DataTable in JSF. Below implementation is sorting the list based on 'actualGADate', now I want to sort it based on prodBuild property which are nothing but the versions


ProdVersionTable.java
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Amitkumar Fulambarkar wrote:I am iterating the list with DataTable/ Below implementation is sorting the list based on 'actualGADate', now I want to sort it based on prodBuild property which are nothing but the versions


You seem to have completely ignored my suggestions.

You haven't described your sorting rules.

You haven't indicated what specific problems you're having.

Additionally, you posted a huge amount of irrelevant code, which many people will not want to read. You should provide an SSCCE that has just enough code to show what you tried, with nothing extra. For instance:


We don't need or want to see 8 million irrelevant getters and setters and pointless comments.

If you want help, you need to pay attention to what people are saying and follow their suggestions.
Amitkumar Fulambarkar
Greenhorn

Joined: Sep 17, 2008
Posts: 29
Hi Jeff,


You seem to have completely ignored my suggestions.


I'm sorry if I missed anything in the post.
You haven't described your sorting rules.

I want to display the version numbers in the order I sent. If we consider them as numeric and sort, it will not give expected result. I wanted to know if there is any API to do this kind of sorting like compare() method or I will have to write custom code. I am not sure what kind of rule this sorting falls in.
You haven't indicated what specific problems you're having.

The problem here is that I can sort the Date as we have the compare method. I want to know the same for String that can give me this kind of sorting. In the above code, there are chances that actualGAdate is null which will through null pointer exception when I call compare() or any other method on this. Hence want to sort the version numbers.

Thank you all for your inputs.



Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
#1 - You almost certainly don't want to compare Date objects via ==, but by using the methods in the Date class.

#2 - As was said before, you need to write code that compares String objects according to your rules.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Amitkumar Fulambarkar wrote:Hi Jeff,


You seem to have completely ignored my suggestions.


I'm sorry if I missed anything in the post.
You haven't described your sorting rules.

I want to display the version numbers in the order I sent.


But that's just one small example. You still need to clearly spell out your rules.

You haven't indicated what specific problems you're having.

The problem here is that I can sort the Date as we have the compare method.


What does Date have to do with it?

I want to know the same for String that can give me this kind of sorting.


You can look at the docs for String and see that, like Date, it is Comparable, and therefore that it has a defined "natural" sort order and a compareTo() method. However, that doesn't mean that the natural ordering will be the one you want.

In the above code, there are chances that actualGAdate is null which will through null pointer exception when I call compare() or any other method on this. Hence want to sort the version numbers.


Do you want to sort by date or by version number? If you want to sort by date, then do so, and avoid the NPE by writing a Comparator that checks for null first, and puts the nulls at the beginning or at the end. It won't throw NPE unless you let it do so.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

What are you trying to do? You say you want to sort using version number and doing comparison for date? Are you sure about what you are doing and what you are asking? We do not know anything about your application, so be specific when you are asking questions.


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

Amitkumar Fulambarkar wrote:I want to display the version numbers in the order I sent.

We are asking you to define/write down the sorting order in such a way that you could hand them to an intern, as well as a bunch of cards with the various numbers on them, and the intern would understand how to sort them.

It needs to be clear and un-ambiguous. The problem with "I want to display the version numbers in the order I sent." is that it tells me NOTHING about where to put a number like "2.97.1293". Your rules ONLY tell me how to sort the five number you gave in your original post, and gives no clue on how you determined that order.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

fred rosenberger wrote:
Amitkumar Fulambarkar wrote:I want to display the version numbers in the order I sent.

We are asking you to define/write down the sorting order in such a way that you could hand them to an intern, as well as a bunch of cards with the various numbers on them, and the intern would understand how to sort them.

It needs to be clear and un-ambiguous. The problem with "I want to display the version numbers in the order I sent." is that it tells me NOTHING about where to put a number like "2.97.1293". Your rules ONLY tell me how to sort the five number you gave in your original post, and gives no clue on how you determined that order.


Exactly, tell us the exact problem statement.
Amitkumar Fulambarkar
Greenhorn

Joined: Sep 17, 2008
Posts: 29
My apology. I think I posted the question assuming that you guys will understand how we decide the version containing major and minors.

There is no logic as such. I am trying to explain it here. I released a product of version 1.00, then I release its minor version, I put it as 1.01, then 1.02. Then I had another version which took say 2.00, then there minor versions like 2.10,2.20 ..... 10.00.01, 10. etc. this is how we give the major and minors for any product we release.


Also, my current implementation is using the date for sorting and I want to do it on version numbers that I given as sample in my first post.

Hope this explains my question.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Amitkumar Fulambarkar wrote:My apology. I think I posted the question assuming that you guys will understand how we decide the version containing major and minors.


Never assume that somebody is thinking the same way you are. Especially somebody who doesn't know you and doesn't know anything about what you're working on, what your requirements are, etc. What may be totally obvious and intuitive to you might never have occurred to somebody else, and he might be thinking something completely different is totally intuitive and obvious.

There is no logic as such.


Then it is impossible to write the code.

You cannot write the code until you define and understand the rules and can describe them clearly, precisely, and completely.

I am trying to it here. I released a product of version 1.00, then I release its minor version, I put it as 1.01, then 1.02. Then I had another version which took say 2.00, then there minor versions like 2.10,2.20 ..... 10.00.01, 10. etc. this is how we give the major and minors for any product we release.


Again: That just tells us how you would sort that particular handful of values. It doesn't tell us the rules for the general case. With a complete enough set of examples, and some common sense, people might be able to infer the pattern, but I think it's better if you make the effort to spell it out. As an adjunct to the explanation of the rules, a couple of examples are useful, of course.

I think I have an idea of what you want to do, but I'm not going to state it. Clearly specifying the requirements and the algorithm is at least as big a part of programming as learning the language syntax and libraries. Probably bigger.

Also, my current implementation is using the date for sorting and I want to do it on version numbers that I given as sample in my first post.


Okay, so the date is totally irrelevant and there's no need to further confuse things by mentioning it again.
Amitkumar Fulambarkar
Greenhorn

Joined: Sep 17, 2008
Posts: 29
Thank you Jeff and all for your valuable inputs. I will try to figure out how I can solve this.

Thanks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

Amitkumar Fulambarkar wrote:I think I posted the question assuming that you guys will understand how we decide the version containing major and minors.

WE do understand what you mean.

The problem is, the JVM does not. Your job, as a coder, is to tell the JVM how to do it. And you can't do that until you can tell yourself (or me, or Jeff, or an intern...).

What we are trying to do is for you to think through the problem. Present what you come up with as your algorithm, then we can help you refine it and point out and end or corner cases you may have missed.

The idea is that once you have the algorithm written down in English (or Spanish, or French or whatever natural language you choose), converting that to Java is easy. But if you can't tell ME how to do it, there is no way you can tell the JVM how to do it.
Sabeer Abdul Khadir
Greenhorn

Joined: Dec 13, 2013
Posts: 5

Amitkumar Fulambarkar wrote:Hi,

Can someone please advise how to sort the version numbers ascending or descending orders. Please provide some sample code snippet.

3.10.01
3.2.01
3.1.03
3.1.02
3.1.01




Hi Amit.

Sorry for an extremely late response. Recently I had to face the same question in an interview and this made me to think for a solution. Like everyone else, I googled and couldnt find much help. So finally I created something which I would like to share . I hope this will help some one some day, if not you

Create a class to hold your version and create a comparator which can be used to sort this class once you populate this into a list.
Override the compare method in the comparator like below.


and the compareValues(String[] array1, String[] array2) implementation goes like this.




Create a list of versions


Now you can make use of the

One request to all, Please revert back to me for any gaps or bugs in the code.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Welcome to the Ranch

Did your code work?
Sabeer Abdul Khadir
Greenhorn

Joined: Dec 13, 2013
Posts: 5

Campbell Ritchie wrote:Welcome to the Ranch

Did your code work?


Yeah It worked..But i want to see whether I left any holes in my code thru which bugs can sneak thru.. Please help me by putting your thoughts Ritchie

Sorry people, I forgot to add what getVersionAsArray() is, it is nothing but a method in the Version class which returns something like

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
The usual Java™ technique is to use a Comparator. Why didn't you?
Sabeer Abdul Khadir
Greenhorn

Joined: Dec 13, 2013
Posts: 5

Campbell Ritchie wrote:The usual Java™ technique is to use a Comparator. Why didn't you?

what did you meant by 'didn't you". ? Sorry, I couldnt make out anything from that. ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
I meant, why didn't you write a Comparator<VersionNumber>?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Campbell Ritchie wrote:I meant, why didn't you write a Comparator<VersionNumber>?

He did.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Damn! I got that really wrong, didn't I. Sorry
Sabeer Abdul Khadir
Greenhorn

Joined: Dec 13, 2013
Posts: 5

Campbell Ritchie wrote:I meant, why didn't you write a Comparator<VersionNumber>?


Thanks Burkett for the response.
@Campbell: I did and it is that comparator<Version > is only having the above written implementation.
Sabeer Abdul Khadir
Greenhorn

Joined: Dec 13, 2013
Posts: 5

Hi All,

There indeed was a problem in my code and Im leaving it for you guys to figure out ;). Anyways im tweaking my code a little bit.
The compare method goes like this



and the compareValues(String[] s1, String[] s2) implementation goes like this



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting version numbers