?
Solved

Sorting the values of hashtable

Posted on 2003-11-23
1
Medium Priority
?
269 Views
Last Modified: 2010-03-31
This is my part of code..I have hashtable, docIdTable.
CosSimilarity class has two members, DocId and Similarity.
I want to display with sorted Similarity(mainly) and DocId.
Based on this code, it will display..

DocID     Sim
344       33.44
323       23.22
567       12.23
766       12.23
345       12.23

It will sort by similarity. If same similarity, how can I sort by DocID? Thank you

DocID     Sim
344       33.44
323       23.22
345       12.23
567       12.23
766       12.23

ArrayList toArrayList = new ArrayList(docIdTable.values());
object[] toArray = toArrayList.toArray();
Arrays.sort(toArray);
            
for(int i=0; i<toArray.length; i++){
             buf.append(((CosSimilarity)toArray[i]).getDocId());
             buf.append(((CosSimilarity)toArray[i]).getSimilarity());
}                              



public int compareTo(Object o) {
   if (o instanceof CosSimilarity) {
       CosSimilarity t = (CosSimilarity) o;
       return compareTo(t);
   }throw (new ClassCastException());
}
                  
public int compareTo(CosSimilarity t) {                                    if(t.similarity > this.similarity){                                 
                        return 1;
                }else if(t.similarity == this.similarity){
              return 0;
      }else{
                return -1;
                }
 }            
                  
0
Comment
Question by:horizzang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
smitty22 earned 200 total points
ID: 9808233
Write another compareTo method that compares DocIDs.  Modify your compareTo( Object o ) method so that if compareTo( CosSimilarity t ) returns 0, it makes a call to compareTo( DocID d ) to determine the relation.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question