• 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

 
Ranch Hand
Posts: 937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This really has nothing to do with servlets, so I've moved it to the JDBC forum for you.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or just implement a quick sort algo in javascript?
 
sunitha reghu
Ranch Hand
Posts: 937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<<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
Posts: 925
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic