Results 1 to 4 of 4
- 01-06-2011, 05:23 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
Sort by Word frequency and alphabetically
hi there i need quick help.
I have got this code:
This doesn't sort by word frequency or alphabetically. :/ Also i want to write the code with arrow in different way if possible.Java Code:[B]public String countAndSort(Vector<String> a){ for (int i=0; i<a.size();i++) { Integer freq = m.get(a.elementAt(i)); m.put(a.elementAt(i), (freq == null) ? 1 : freq + 1); } // System.out.println(m.size() + " distinct words:"); ArrayList<Map.Entry<String, Integer>> word = new ArrayList<Map.Entry<String, Integer>>(m.entrySet()); Collections.sort(word, [COLOR="Red"]new Comparator<Map.Entry<String, Integer>>()[/COLOR]); [COLOR="Red"]RED = doesn't work;[/COLOR] System.out.println(m); return ""; } } @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { int c1 = o1.getValue().intValue(); int c2 = o2.getValue().intValue(); if (c1 < c2) { return -1; } else if (c1 > c2) { return 1; } else { // If counts are equal, compare keys alphabetically. return o1.getKey().compareTo(o2.getKey()); } } [/B]
Example:
Say that vector has "It was the best of times, it was the worst of times," then it prints out {of=2, it=2, times=2, worst=1, best=1, the=2, was=2}Last edited by Fubarable; 01-06-2011 at 06:23 PM. Reason: Moderator Edit: code tags added
- 01-06-2011, 06:15 PM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Hi - can you please use the code tags and post your code in more readable format. With its current format its difficult to read/understand.
Best,--user0--
-
Moderator edit: code tags added to original post
-
OP, get rid of the comments that aren't part of your real code and let's see your real code. What you've posted doesn't look like it is valid syntax for an anonymous inner class, but it's verry difficult to tell with the extra stuff you've added to your post. Also, I agree, please use code tags (see the link in my signature below). If your code doesn't compile, then post your exact error messages and indicate with a valid Java comment where the error occurs. Same for run-time exceptions.
Similar Threads
-
Arraylist problem Sort Date , alphabetically
By ob3lix in forum New To JavaReplies: 1Last Post: 11-26-2010, 03:55 PM -
Count the frequency of the word in a text file instead of a sentence.
By bMorgan in forum New To JavaReplies: 3Last Post: 11-09-2010, 12:10 AM -
Sort Frequency
By ScaryJello in forum New To JavaReplies: 7Last Post: 03-26-2009, 12:47 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM -
Largest string value (alphabetically)
By mew in forum New To JavaReplies: 3Last Post: 12-14-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks