Solved

Sorting the values of hashtable

Posted on 2003-11-23
1
265 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
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 71
java stored proc example 9 44
hibernate example issues from command prompt 10 50
Java class and jar 3 19
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

789 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