Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting the values of hashtable

Posted on 2003-11-23
1
Medium Priority
?
270 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

604 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