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?
See Question&Answers more detail:
os