+ 1
Why the sorting function is not working?Input only the numbers (decimals) is the function only checking for the first character?
import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); String n=sc.nextLine(); String arr[]=n.split(","); Arrays.sort(arr); System.out.println(Arrays.toString(arr));
7 Réponses
+ 2
Jayakrishna🇮🇳 and Cherrim
import java.util.*;
public class Program
{
public static void main(String[] args)
{
Scanner sc=new Scanner (System.in);
String n=sc.nextLine();
String arr[]=n.split(",");
double a[]=new double[arr.length] ;
for(int i=0;i<arr.length;i++)
a[i]=Double.parseDouble(arr[i]);
Arrays.sort(a);
for(double e:a)
System.out.println(e);
}}
//Working now
+ 3
Your applying sort on string array. So it sort on alphabetical charecters sorting..
for example:
aa comes before ab
11 comes before 2
0
So how to get the desired output?
0
int[] arr?
0
Cherrim I need to convert it into double. So I don't think so there is a need of int. But I maybe wrong too. Can you please elaborate your answer?
0
What are your trying to achieve?
Sort decimal values in ascending order?
0
Cherrim yes