Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do sorting using java

Tags:

java

sorting

I have text file with list of alphabets and numbers. I want to do sorting w.r.t this number using java.

My text file looks like this:

a--->12347
g--->65784
r--->675

I read the text file and i split it now. But i dont know how to perform sorting . I am new to java. Please give me a idea.

My output want to be

g--->65784
a--->12347
r--->675

Please help me. Thanks in advance.

My coding is

String str = "";
BufferedReader br = new BufferedReader(new FileReader("counts.txt"));
while ((str = br.readLine()) != null) {
String[] get = str.split("---->>");

When i search the internet all suggest in the type of arrays. I tried. But no use.How to include the get[1] into array.

    int arr[]=new int[50]
    arr[i]=get[1];
    for(int i=0;i<50000;i++){
                for(int j=i+1;j<60000;j++){
                   if(arr[i]>arr[j]){
                       System.out.println(arr[i]);
                   }
                }
like image 253
user354419 Avatar asked Sep 17 '26 00:09

user354419


1 Answers

You should use the Arrays.sort() or Collections.sort() methods that allows you to specify a custom Comparator, and implement such a Comparator to determine how the strings should be compared for the purpose of sorting (since you don't want the default lexicographic order). It looks like that should involve parsing them as integers.

like image 81
Michael Borgwardt Avatar answered Sep 18 '26 14:09

Michael Borgwardt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!