Here you can find the source of compareVersion(String va, String vb)
Parameter | Description |
---|---|
va | version number 1. |
vb | version number 2. |
public static int compareVersion(String va, String vb)
//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; } }