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.?
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.
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.)
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.
Joined: Dec 12, 2002
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?
Joined: Jun 26, 2002
<<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.