Why the sorting function is not working?Input only the numbers (decimals) is the function only checking for the first character? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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));

17th Jul 2021, 7:11 PM
Atul [Inactive]
7 Answers
+ 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
18th Jul 2021, 4:40 AM
Atul [Inactive]
+ 3
Your applying sort on string array. So it sort on alphabetical charecters​ sorting.. for example: aa comes before ab 11 comes before 2
17th Jul 2021, 7:40 PM
Jayakrishna 🇮🇳
0
So how to get the desired output?
17th Jul 2021, 7:59 PM
Atul [Inactive]
0
int[] arr?
17th Jul 2021, 8:08 PM
Rohit
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?
17th Jul 2021, 8:11 PM
Atul [Inactive]
0
What are your trying to achieve? Sort decimal values in ascending order?
17th Jul 2021, 8:15 PM
Rohit
0
18th Jul 2021, 3:31 AM
Atul [Inactive]