I have some data like this:

6 9 5 2 3 6 1 5	1
0 3 4 5 3 9	1
9 4 3 8 6 8 5	1
5 4 9 8 1 4 4 5	1
5 8 6 7 2 5 5 6	1
8 5 3 9 0 2	1
6 8 4 5 1 6	1
7 1 3 6 9 7 4 4	1
9 9 9 8 5 0 6 7	3
9 9 9 8 5 0 6 6	2
6 1 9 9 8 8 0 4	1
9 9 9 8 5 0 6 5	1
8 3 2 1 2 4	1
2 5 1 8 7 0 1	1
6 7 0 3 1 6	1
5 9 8 9 3 8 1 9	1
8 2 3 7 8 5 9	1
1 8 5 6 9 2 6	1
3	55763
2	79825
8 8 2 9 3 0 3 9 3	1
1	67085

I need to sort the above data (large to small) based on the last column in java. The last column has a tab before it. Some parts are a little different also like "3 55763
2 79825 " --this part.

How to go about it?

There is 1. 2. etc . It just came as I did a code tag around the input data.

This is a small part of a bug text file. I copied a small part of it. After I sort it in excel I get the output as this:

1	96726
2	85751
9	83553
0	81164
5	65309
8	64997
6	64163
7	62598
3	58687
4	55484
1 2	10799
2 9	9563
9 1	8246
3 1	7221
0 0	7006
2 5	6990
5 9	6459
4 0	6060
1 6	5532
6 8	5312
8 7	5175
1 2 9	4995
0 0 0	4765
9 8	4756
0 0 0 0	4026
2 9 1	3973
0 1	3697
1 2 5	3618
5 5	3589
9 1 2	3576
0 0 0 0 0	3404
2 5 9	3363
7 4	3285
7 2	3111
7 7	2983
3 1 2	2921
1 3	2884
8 3	2808
0 0 0 0 0 0	2806
6 1	2778
8 4	2628

Create a Class with attributes the line and the last column of the line:

public class YourClass {
  private String line = null;
  private int lastColumn = 0;
}

Read each line of the file. Use the split method of the String class and take the last "element" that is your last column. Then for each line create an object of the above class, with those values and put them into a Vector.

Now you have a Vector with objects like the above. So you can sort based on the attribute int lastColumn .
For sorting object look at the Comparable interface. Write your own algorithm or use the sort method of the Arrays class

Can you please elaborate on

put them into a Vector

java.uti.Vector or a list, or an array, or any other form of data structure were you can store them in order to sort them

Thanks a lot for the reply. Could you please post the code upon how to do the above task. I am pretty new to Java..

If you are a beginner then this is too much for you, but you can try.
First create the Class that was described with get/set methods and post the code. Then put it aside.

Then write in a main method code that reads a file and print each line. Use the Scanner class:

Scanner scan = new Scanner(new File("file.txt"));
while (scan.hasNextLine()) {
   String line = scan.nextLine();
// print the line
}

Look at the API of the Scanner class. Just search it at the internet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.