Results 1 to 4 of 4
  1. #1
    darpan12 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Sort by Word frequency and alphabetically

    hi there i need quick help.

    I have got this code:
    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]
    This doesn't sort by word frequency or alphabetically. :/ Also i want to write the code with arrow in different way if possible.

    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

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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--

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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

  1. Arraylist problem Sort Date , alphabetically
    By ob3lix in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 03:55 PM
  2. Replies: 3
    Last Post: 11-09-2010, 12:10 AM
  3. Sort Frequency
    By ScaryJello in forum New To Java
    Replies: 7
    Last Post: 03-26-2009, 12:47 AM
  4. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 02:03 PM
  5. Largest string value (alphabetically)
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •