Topic
  • 7 replies
  • Latest Post - ‏2012-08-15T17:43:01Z by gr8yash
SystemAdmin
SystemAdmin
2768 Posts

Pinned topic Doesn't Sort data in Ascending order in HashTable?

‏2006-03-08T11:22:05Z |
Hello

I am using JSP, Servlets. After quering in servlets, putting data in HashTable then set attribute in servlets, get attribute in JSP & retrieving data from HashTable in JSP & putting in select html element, data is not coming in ascending order in select drop down box. I need A,B,C,D but it is coming b,d,c,a. When I run query in DB, it shows in sequence but after putting in HashTable, it is not coming in sequence.

How to make it ascending?

Looking forward for an early reply.

Thanks.
Updated on 2012-08-15T17:43:01Z at 2012-08-15T17:43:01Z by gr8yash
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2006-03-08T20:53:09Z  
    Hashtable doesn't make any guarantees about order. Generally, when you need a dynamically key-sorted order, you use a TreeMap. In your case, since you are getting the data in sequence ( and, I assume, not adding specific elements later that need to be ordered, ) you can use a LinkedHashMap which guarantees insertion order and is faster than a TreeMap. See Map Implementations for more information.

    Another possible alternative is correlated arrays or ArrayLists.
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2006-03-09T08:04:00Z  
    Hashtable doesn't make any guarantees about order. Generally, when you need a dynamically key-sorted order, you use a TreeMap. In your case, since you are getting the data in sequence ( and, I assume, not adding specific elements later that need to be ordered, ) you can use a LinkedHashMap which guarantees insertion order and is faster than a TreeMap. See Map Implementations for more information.

    Another possible alternative is correlated arrays or ArrayLists.
    Hi

    I tried with TreeSet but it didn't work. Here is the code :

    In servlet :

    Connection lConnection = getConnection(pRequest);
    String lSQL = "";
    Statement lStatement;
    ResultSet lResultSet;

    Hashtable lLtypeHashtable = new Hashtable();

    lStatement = lConnection.createStatement();
    lSQL = "SELECT RCID,RCMEANING FROM REFERENCECODES WHERE RCDOMAIN = 'LOCATIONTYPE' AND RCROWSTATE > 0 order by RCMEANING";

    lResultSet = lStatement.executeQuery(lSQL);
    while(lResultSet.next())
    {
    String lRcid = lResultSet.getString(1);
    String lRcmeaning = lResultSet.getString(2);
    lLtypeHashtable.put(lRcid.trim(),lRcmeaning.trim());
    }
    if(lResultSet != null) lResultSet.close();
    if(lStatement != null) lStatement.close();

    pRequest.setAttribute("LtypeHashtable",lLtypeHashtable);

    //Below Query is executed when one data from select element is selected
    String lLType = DisplayUtilities.getString(pRequest.getParameter("LType"),true);
    //LType is name of select element in JSP.
    if (lLType != null)
    {
    lSQL = lSQL + " AND " + lUpperCaseFunction + "(LOCATIONTYPE)" +
    " = " + DBUtilities.formatString(lLType.toUpperCase());
    pRequest.setAttribute("rLType",lLType+"");
    }

    In JSp :

    <%
    Hashtable lLtypeHashtable = (Hashtable)request.getAttribute("LtypeHashtable");
    %>

    <TR>
    <TD width="15%">

    <select name="LType" size="1" >
    <option Value="">< Select ></option>
    <%
    if(lLtypeHashtable != null)
    {
    Enumeration enum = lLtypeHashtable.keys();
    while(enum.hasMoreElements())
    {
    String key = (String)enum.nextElement();
    String value = (String)lLtypeHashtable.get(key);
    String flagBack = "";
    if(key.equals((String)request.getAttribute("rLType")))
    {
    flagBack = "selected";
    }
    %>
    <option Value="<%=key%>" <%=flagBack%>><%=value%></option>
    <%
    }
    }
    %>
    </select>
    </TD>
    </TR>

    How should I implement TreeSet?

    Looking forward for an early reply.

    Thanks.
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2006-03-09T13:32:10Z  
    Hashtable doesn't make any guarantees about order. Generally, when you need a dynamically key-sorted order, you use a TreeMap. In your case, since you are getting the data in sequence ( and, I assume, not adding specific elements later that need to be ordered, ) you can use a LinkedHashMap which guarantees insertion order and is faster than a TreeMap. See Map Implementations for more information.

    Another possible alternative is correlated arrays or ArrayLists.
    I tried thru TreeMap but its sorting thru key(ID) & not values. I need alphabetically sorting of values(data) in select drop down box such that in future also some new topics are added to select, it should show alphabetically in select box.
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2006-03-10T12:31:47Z  
    I tried thru TreeMap but its sorting thru key(ID) & not values. I need alphabetically sorting of values(data) in select drop down box such that in future also some new topics are added to select, it should show alphabetically in select box.
    As I indicated in my initial response, LinkedHashMap "guarantees insertion order and is faster than a TreeMap." That should work for you. The values are pulled from the database in the order you want them, so LinkedHashMap's insertion order here is also value-alphabetical. The code example you sent still uses HashTable.
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2006-03-11T10:51:32Z  
    As I indicated in my initial response, LinkedHashMap "guarantees insertion order and is faster than a TreeMap." That should work for you. The values are pulled from the database in the order you want them, so LinkedHashMap's insertion order here is also value-alphabetical. The code example you sent still uses HashTable.
    I can't use LinkedHashMap because data is not in order in DB & in future more data can be added in select box.

    Anyway, Thanks, problem got solved. It happened by implementing Comparator interface, comparing values, LinkedList, ListIterator.
  • SystemAdmin
    SystemAdmin
    2768 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2006-03-13T13:18:10Z  
    I can't use LinkedHashMap because data is not in order in DB & in future more data can be added in select box.

    Anyway, Thanks, problem got solved. It happened by implementing Comparator interface, comparing values, LinkedList, ListIterator.
    I'm glad you got the issue resolved. I had understood from your code
    code
    order by RCMEANING
    [/code]
    that the values were received in sorted order.
  • gr8yash
    gr8yash
    3 Posts

    Re: Doesn't Sort data in Ascending order in HashTable?

    ‏2012-08-15T17:43:01Z  
    Use TreeMap to have all elements in sorted order. Refer
    Java TreeMap Examples
    for more details.