0

I have input as:

String[] a = {"1.0.0","13.2.4","1.0.1","0.0.0","2.3.4","1.1.2","12.2.2","12.2.1"};

I want the output as:

{0.0.0, 1.0.0, 1.0.1, 1.1.2, 2.3.4, 12.2.1, 12.2.2, 13.2.4};

I'm stuck where I can't find a way to compare two elements. My code just compare once instead of comparing all the elements:

public static String[] compare(String[] a) {
    String temp;
    String[] a1;
    String[] a2;

    for (int i = 0; i < a.length - 1; i++) {
        a1 = a[i].split("\\.");
        a2 = a[i + 1].split("\\.");

        for (int j = 0; j < a1.length; j++) {
            int v1 = j < a1.length ? Integer.parseInt(a1[j]) : 0;
            int v2 = j < a2.length ? Integer.parseInt(a2[j]) : 0;

            if (v1 > v2) {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                j = a1.length;
            } else if (v1 == v2) {
                continue;
            } else {
                j = a1.length;
            }
        }
    }
    return a;
}
Community
  • 1
  • 1
henil shah
  • 11
  • 1

3 Answers3

0

You can split each string into an array by dots and sort them sequentially by the integer values in the columns of these arrays using a chain of comparators:

String[] a = {
        "1.0.0", "13.2.4", "1.0.1", "0.0.0",
        "2.3.4", "1.1.2", "12.2.2", "12.2.1"};

String[] b = Arrays
        // Stream<String>
        .stream(a)
        // split a string into an array by dots
        // return Stream<String[]>
        .map(str -> str.split("\\."))
        // sort string arrays by columns: first, second and last
        .sorted(Comparator // using a chain of comparators
                .<String[]>comparingInt(arr -> Integer.parseInt(arr[0]))
                .thenComparingInt(arr -> Integer.parseInt(arr[1]))
                .thenComparingInt(arr -> Integer.parseInt(arr[2])))
        // join an array of strings back into a single string
        // return Stream<String>
        .map(arr -> String.join(".", arr))
        // return sorted array
        .toArray(String[]::new);

// output
System.out.println(Arrays.toString(b));
// [0.0.0, 1.0.0, 1.0.1, 1.1.2, 2.3.4, 12.2.1, 12.2.2, 13.2.4]

See also: How to sort a character by number of occurrences in a String using a Map?

0

Here is one option. I used an exaggerated heading list for demo purposes. Yours should work okay too.

  • First it splits the levels into arrays of strings.
  • Then it uses those arrays to calculate the maximum level that exists based on the number of section numbers.
  • It then uses the number of levels to build comparator sufficient to compare all levels.
  • And then it sorts the arrays of arrays (they're already split) using the comparator and rejoins them into the original section heading.
String[] sections = { "1.0.0", "13.2.4", "1.0.1", "0.0.0", "2.3.4",
        "1.1.1.1.2.2", "12.2.2.2", "12.2.1" };

// split up the levels into an array of arrays.
String[][] levels =
        Arrays.stream(sections).map(str -> str.split("\\."))
                .toArray(String[][]::new);

// calculate the maximum level
int maxLevel = 0;
for (String[] arr : levels) {
    maxLevel = Math.max(maxLevel, arr.length);
}

// now use that to build the comparator.
Comparator<String[]> comp = Comparator
        .comparingInt(arr -> Integer.parseInt(arr[0]));
for (int i = 1; i < maxLevel; i++) {
    final int k = i;
    comp = comp.thenComparingInt(
            arr -> Integer.parseInt(arr[k]));
}

// and then sort them and rejoin the numbers.
String[] result = Arrays.stream(levels).sorted(comp)
        .map(arr -> Arrays.stream(arr)
                .collect(Collectors.joining(".")))
        .toArray(String[]::new);


Arrays.stream(result).forEach(System.out::println);

Prints

0.0.0
1.0.0
1.0.1
1.1.1.1.2.2
2.3.4
12.2.1
12.2.2.2
13.2.4
WJS
  • 36,363
  • 4
  • 24
  • 39
-1

enter image description here

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Test implements Comparable {
    int a;
    int b;
    int c;
    public Test(String s) {
        String []s1 = s.split("\\.");

        this.a = Integer.parseInt(s1[0]);
        this.b = Integer.parseInt(s1[1]);
        this.c = Integer.parseInt(s1[2]);
    }
    public String toString () {
        return a + "." + b + "." + c;
    }

    public static void main (String[] args) {
        String []a = {"1.0.0", "13.2.4","1.0.1","0.0.0","2.3.4", "1.1.2","12.2.2","12.2.1"};
        List<Test> tests = Arrays.stream(a).map(Test::new).collect(Collectors.toList());
        System.out.println(tests);
        Collections.sort(tests);
        System.out.println(tests);
    }

    @Override
    public int compareTo(Object o) {
        Test t = (Test) o;
        if (t.a != a) return a - t.a;
        if (t.b != b) return b - t.b;
        return c - t.c;
    }
}

Create a class, a list and a comparator.

mehdim2
  • 129
  • 9