hi one and all, I HAVE A TEXT FILE LIKE THIS student id marks 001 154 002 144 003 132 004 175 005 163 : : : : LIKE THAT. now i have to sort the entire data according to the marks in increasing order.should it be wriiten in a normaal way using if else or is there any specific methods for easy approach and quicker evaluation for such operations like sort().i read thru the sort, set and collections but i could not make out how to implement them with my problem.respond ASAP please.thanx in advace. bye
If the purpose of this assignment is for you to begin to think about creating a sorting algorithm, then perhaps you'd do well to do that instead. Otherwise, you could make use of Arrays.sort(Object) or Collections.sort(List) pretty easily.
1. use a BufferedReader to read each line from the file 2. use a StringTokenizer to split each line into an ID and a score 3. create a Score object (create a new class) and store ID & score in it 4. implement the Comparable interface on Score to order by score 5. add each Score object to a List object (e.g. ArrayList) 6. use the Collections sort method 7. use a BufferedWriter to write a string representation of each object in the collection back to the file [ February 11, 2004: Message edited by: Jeff Langr ]
Elaborating a little more on what Jeff has posted (especially since you said that you read through the set, collections & sort but didn't quite understand): Comparable interface defines a method compareTo() which takes an argument of type Object & returns an int which tells whether the object on which the compareTo() method was called is less than or equal to or greater than or the argument Object. So, any class that implement the Comparable interface must provide an implementation for the compareTo() method. In this method, you can compare the 2 objects - argument object & the object on which the compareTo() method was invoked - in whatever way you want. Another way to provide for sorting is to create a class that implements the Comparator interface. The class that implements this interface must provide implementation for 2 methods - compare() & equals(). Then, you can call the static method sort() of Collections class & pass it the collection that needs to be sorted and the sorting comparator that you want it to use to order the objects in that collection.
Here's something to play around with (I've simulated the data from the file as an array)
Joined: Jan 30, 2004
hi all, thanx for ur response and above code,but i have a problem with it as my file is very huge like i have datas as much as 19000 student id & marks ID Marks Grade 001 154 B 002 144 C 003 132 D 004 175 A 005 163 B : : : : : : 19016 152 B and in the above code if i break and pass it in string array it gives array out of bound exception as its too large.so how the above code could be modified to overcome this problem and sort the above data based on marks. thanx in advance