Solved

Sorting the values of hashtable

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now