Java String Reverse compareVersion(String va, String vb)

Here you can find the source of compareVersion(String va, String vb)

Description

Compare version number, the format of version number should be X.X.X style.

License

Open Source License

Parameter

Parameter Description
va version number 1.
vb version number 2.

Declaration

public static int compareVersion(String va, String vb) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w w w.j  a  v  a2s  .  co m*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Compare version number, the format of version number should be X.X.X
     * style.
     * 
     * @param va
     *            version number 1.
     * @param vb
     *            version number 2.
     * @since 2.3
     */
    public static int compareVersion(String va, String vb) {
        String[] vas = va.split("\\."); //$NON-NLS-1$
        String[] vbs = vb.split("\\."); //$NON-NLS-1$

        List<String> vaList = new ArrayList<String>();
        for (int i = 0; i < vas.length; i++) {
            vaList.add(vas[i].trim().equals("") ? "0" : vas[i]); //$NON-NLS-1$ //$NON-NLS-2$
        }
        List<String> vbList = new ArrayList<String>();
        for (int i = 0; i < vbs.length; i++) {
            vbList.add(vbs[i].trim().equals("") ? "0" : vbs[i]); //$NON-NLS-1$ //$NON-NLS-2$
        }

        if (vas.length < vbs.length) {
            for (int i = vas.length; i < vbs.length; i++) {
                vaList.add("0"); //$NON-NLS-1$
            }
        } else if (vas.length > vbs.length) {
            for (int i = vbs.length; i < vas.length; i++) {
                vbList.add("0"); //$NON-NLS-1$
            }
        }

        for (int i = 0; i < vaList.size(); i++) {
            int a = Integer.valueOf(vaList.get(i)).intValue();
            int b = Integer.valueOf(vbList.get(i)).intValue();
            if (a == b) {
                continue;
            } else {
                return a - b;
            }
        }

        return 0;
    }
}

Related

  1. getReverseShelfKey(String shelfkey)
  2. reverse(String aPackage)
  3. reverse(String s)
  4. reverse(String text)