• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting data in increasing order

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's something to play around with
(I've simulated the data from the file as an array)
 
sina milka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic