my dog learned polymorphism*
The moose likes JDBC and the fly likes sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sorting" Watch "sorting" New topic
Author

sorting

sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
I have four fields ( ssn, last name, first name and wages) .I need to sort either by ssn ,last name, frst name or wage. I am taking these columns frm db as arrays. No of records can be max 250. Can anyone tell me which method will be efficient . If a user clicks sort by first name should i make to trip to db again. or should i use comparator. Also user has the option to delete a record. If the user clicks delete the record what will be bext way to do that.?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This really has nothing to do with servlets, so I've moved it to the JDBC forum for you.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Claus Hetzer
Greenhorn

Joined: Jan 27, 2004
Posts: 1
Well...I would try to minimize the number of times you retrieve data from the DB, especially if you are retrieving all the data every time anyway. The most robust way I can think of to do this would be to set up some kind of object that contains first name, last name, etc., create Comparators for each of the four fields, and when you retrieve the data put the objects, one for each DB record, into a List which can then be sorted using Collections.sort( List, Comparator ). The problem with using arrays is that, while you can use Arrays.sort(), you need to have a way to reorder the other arrays at the same time to maintain data integrity. Better I think to use OOP and Collections from the beginning.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Sunitha,
If you have more than one user that could potentially be looking at these fields, you should consider using a cache. Then all requests could be routed through the cache. Also, a cache gives you a centralized place to update when a deletion occurs. You know that you need to delete the record from the database and the cache (rather than who knows how many private lists.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Whether you decide to cache or resubmit the query the FormattedDataSet gives you the ability to sort by clicking on column headers, with minimal ammount of coding. In fact there is a live demo that allows you to do exactly what you are asking. Code is also available at the demo site.
Select 'Live Demo' from the top, left of http://www.fdsapi.com. Then select Sortable Query'. Use the given query or enter your own. Any query you enter can be sorted, by clicking on column headers.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
SJ Adnams
Ranch Hand

Joined: Sep 28, 2001
Posts: 925
Or just implement a quick sort algo in javascript?
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
well if its only first name and last name its easy to do using collections
but the problem is i have four arrrays first name, last name, ssn and wages
As Claus has mentioned I cant use Array.sort for each array coz of data integrity
The max number of records is 250 for each user. So I am going to experiment with DB and let me see how things will work.

SJ, tell me how to do in js any sample code?
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
<<well if its only first name and last name its easy to do using collections
but the problem is i have four arrrays first name, last name, ssn and wages
As Claus has mentioned I cant use Array.sort for each array coz of data integrity>>
What you really have is a 2 dimensionaly array of Objects (i.e. Object[][]). The FormattedDataSet example I pointed you to converts a ResultSet into Object[][] and sorts it with very few lines of code for ANY ResultSet. I have a class called the ArrayComparator that does this behind the scenes. Javadocs and other examples are at http://www.fdsapi.com. It has a number of signatures that follow.
[code]
Object[][] data=...;
// sort data in array by column 0 in descending order.
ArrayComparator.sort(data, 0, "desc");
// Sort by multiple columns. Much like the SQL 'order by' clause with multiple columns.
ArrayComparator ac=new ArrayComparator();
ac.addSortCol(0,"asc"); // sort by column 0 in ascending order first
ac.addSortCol(1,"desc"); // then by column 1 in descending order
ac.addSortCol(2,"asc"); // then by column 2 in ascending order
ac.sort(array); // note ac can be used to sort as many arrays as needed after it has been created.

//You can combine the ResultSetConverter (a thin wrapper for a ResultSet that converts the ResultSet into Object[][]
ResultSetConverter rsc=new ResultSetConverter(resultSet);
Object[][] resultSetData=rsc.getResultSet();
// Sort the data in the ResultSet by col 3 (i.e. arrays start at 0) in ascending order
ArrayComparator.sort(resultSetData, 2, "asc");
[code]
If you use the FormattedDataSet getSortedText code you can do exactly what you want with a couple lines of code.
SJ Adnams
Ranch Hand

Joined: Sep 28, 2001
Posts: 925
there is a site called google that allows you to seach for things like "javascript quicksort demo"
this on is cool
http://www.thewebtier.com/sandwich/sandwichSelectionFast.jsp
 
Consider Paul's rocket mass heater.
 
subject: sorting
 
Similar Threads
Problems w/ a HashMap
servlets(help)
servlets
servlet(delete)
Program on Text Files