Solved

Sorting the values of hashtable

Posted on 2003-11-23
1
268 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 50 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

707 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