简体   繁体   中英

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?

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));
  }
}

I think what you want is imposible to achive. Consider this simple example:

public static void main(String[] args) {
    double d = 0.1;
    System.out.println(d);
    d = 0.10;
    System.out.println(d);
    d = 0.100;
    System.out.println(d);
}

prints:

0.1
0.1
0.1

for java 0.1 double is the same as 0.10 and 0.100. There is no double 0.1, 0.10, 0.100 etc, there is only 0.1

您可以编写一个首先将值转换为double并将它们进行比较的Comperator,如果它们等于“0.10”和“0.1”,则应按长度进行比较。

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM