Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java version number sort

String[] k1 = {"0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1", "2.10", "2", "2.2", "2.1"};
double[] k2 = {0.10, 0.2, 0.1, 0, 1.10, 1.2, 1.1, 1, 2.10, 2, 2.2, 2.1};
Arrays.sort(k1);
Arrays.sort(k2);
System.out.println(Arrays.toString(k1));
System.out.println(Arrays.toString(k2));

output:

[0,   0.1, 0.10, 0.2,  1,   1.1, 1.10, 1.2,  2,   2.1, 2.10, 2.2]
[0.0, 0.1, 0.1,  0.2,  1.0, 1.1, 1.1,  1.2,  2.0, 2.1, 2.1,  2.2]

What I would like to have,

[0, 0.1, 0.2, 0.10, 1, 1.1, 1.2, 1.10, 2, 2.1, 2.2, 2.10]

First sort before decimals and after. Like 1, 1.1, 1.2, 1.10, 2, 2.1, etc.

How can I write a comparator for this?

like image 570
Praveen Avatar asked Dec 04 '13 12:12

Praveen


People also ask

How do I compare versions in Java?

Compare two version numbers version1 and version2. If version1 > version2 return 1; if version1 < version2 return -1; otherwise return . You may assume that the version strings are non-empty and contain only digits and the . character.

How can I sort a list of versions of a string?

If you want to sort some versions (e.g. 0.4.3, 5.3.5, 1.2.4) you could use my approach which includes using a java.util.Comparator. To use this you have to use a sorting method (e.g. Arrays.sort (new String [] {"0.4.3", "5.3.5", "1.2.4"}, new VersionComparator ()) ). The VersionComparator class is written below:

How to sort a list in Java?

We can use the following methods to sort the list: Java Stream interface provides two methods for sorting the list: Stream interface provides a sorted () method to sort a list. It is defined in Stream interface which is present in java.util package.

How to compare the two versions of a list in Java?

Implement a method to compare the two versions. If there are more than two versions, then the below versionCompare method can be used as a compare method of sort method, which will sort all versions according to the specified comparison. while (i < v1.length () && v1 [i] != '.') { while (j < v2.length () && v2 [j] != '.') {

What is a version number and how to find it?

A version number is a string that is used to identify the unique state of a software product. A version number looks like a.b.c.d, where a, b, etc are numbers, so the version number is a string in which numbers are separated by dots.


2 Answers

2.1, 2.2, 2.10]

Since 2.10 is greater than 2.2 in this order it looks like a version number sort:

import java.util.Arrays;
import java.util.Comparator;

public class VersionNumberComparator implements Comparator<String> {
  @Override
  public int compare(String version1, String version2) {
    String[] v1 = version1.split("\\.");
    String[] v2 = version2.split("\\.");
    int major1 = major(v1);
    int major2 = major(v2);
    if (major1 == major2) {
      return minor(v1).compareTo(minor(v2));
    }
    return major1 > major2 ? 1 : -1;
  }

  private int major(String[] version) {
    return Integer.parseInt(version[0]);
  }

  private Integer minor(String[] version) {
    return version.length > 1 ? Integer.parseInt(version[1]) : 0;
  }

  public static void main(String[] args) {
    String[] k1 = { "0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1",
        "2.10", "2", "2.2", "2.1" };
    Arrays.sort(k1, new VersionNumberComparator());
    System.out.println(Arrays.asList(k1));
  }
}
like image 97
McDowell Avatar answered Sep 18 '22 18:09

McDowell


package com.poc.sort;

/**
 * @author voyger_india
 *
 */
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

public class TestChapterSort {

    public static void main(String[] args) {
        List<String> chapters = new ArrayList<String>();

        chapters.add("1.1");
        chapters.add("1.2");
        chapters.add("1");
        chapters.add("1.3");
        chapters.add("1.1.1");
        chapters.add("5.6");
        chapters.add("1.1.10");
        chapters.add("4");
        chapters.add("1.1.9");
        chapters.add("1.2.1.10");
        chapters.add("2.1.1.4.5");
        chapters.add("1.2.1.9");
        chapters.add("1.2.1");
        chapters.add("2.2.2");
        chapters.add("1.2.1.11");

        TestChapterSort wer = new TestChapterSort();
        System.out.println(wer.sortChapters(chapters));
    }

    private List<String> sortChapters(List<String> chapters) {
        List<String> sortedChapters = new ArrayList<String>(0);

        int index;
        for (String currChapter : chapters) {
            if (!sortedChapters.contains(currChapter)) {
                index = getInsertIndex(sortedChapters, currChapter);
                sortedChapters.add(index, currChapter);
                System.out.println(sortedChapters);
            }
        }

        return sortedChapters;
    }

    private int getInsertIndex(List<String> sortChapters, String currChapter) {
        int insertIndex = 0;
        if (!CollectionUtils.isEmpty(sortChapters)) {
            int compChapterSub;
            int currChapterSub;
            String[] currChapterAr = currChapter.split("\\.");
            for (String compChapter : sortChapters) {
                String[] compChapterAr = compChapter.split("\\.");
                for (int subLvl = 0; subLvl < 5; subLvl++) {
                    compChapterSub = parseToInt(compChapterAr, subLvl);
                    currChapterSub = parseToInt(currChapterAr, subLvl);
                    if (compChapterSub == currChapterSub) {
                        continue;
                    } else if (compChapterSub == 0 && currChapterSub == 0) {
                        break;
                    } else if (compChapterSub > currChapterSub) {
                        if (checkIfProper(subLvl, compChapterAr, currChapterAr)) {
                            return insertIndex;
                        }
                    }
                }
                insertIndex++;
            }
        } else {
            return 0;
        }
        return insertIndex;
    }

    private int parseToInt(String[] subChapter, int subLvl) {
        try {
            return Integer.parseInt(subChapter[subLvl]);
        } catch (ArrayIndexOutOfBoundsException ae) {
            return 0;
        }
    }

    private boolean checkIfProper(int subLvl, String[] compChapterAr, String[] currChapterAr) {
        int subLvlCk = subLvl - 1;
        int compChapterSub;
        int currChapterSub;
        while (subLvlCk > -1) {
            compChapterSub = parseToInt(compChapterAr, subLvlCk);
            currChapterSub = parseToInt(currChapterAr, subLvlCk);
            if (compChapterSub < currChapterSub) {
                return false;
            }
            subLvlCk--;
        }
        return true;
    }

}
like image 38
user2617914 Avatar answered Sep 19 '22 18:09

user2617914