troubleshooting Question

Sorting numbers and letters using Comparator with Collections.sort

Avatar of kineticexpert
kineticexpertFlag for Australia asked on
JavaJava EE
5 Comments1 Solution849 ViewsLast Modified:
I am trying to sort an ArrayList so that a list of values from a Hashtable are sorted by value, which includes numbers sorted in numerical order and before Characters.

Script below...

Hashtable danHT = new Hashtable();
        danHT.put("15", "15a");
        danHT.put("1", "1a");
        danHT.put("4", "da");
        danHT.put("7", "7a");
        danHT.put("77", "77a");
        danHT.put("2", "2a");
        danHT.put("737", "aaa");
        danHT.put("87", "AAA");
        danHT.put("11", "11a");
        danHT.put("23", "BB23");

        ArrayList myArrayList=new ArrayList(danHT.entrySet());
        Collections.sort(myArrayList, new StringNumberComparator());

        Iterator itr=myArrayList.iterator();
        String key="";
        String value="";
        int cnt=0;
            Map.Entry e=(Map.Entry);
            key = (String)e.getKey();
            value = ((String)e.getValue()).toString();
            System.out.println("Value = " + value + "                         Key: " + key);


    static class MyComparator implements Comparator
        public int compare(Object obj1, Object obj2)
            int result=0;Map.Entry e1 = (Map.Entry)obj1 ;
            Map.Entry e2 = (Map.Entry)obj2 ;//Sort based on values.
            String value1 = (String)e1.getValue();
            String value2 = (String)e2.getValue();
            String word1=(String)e1.getKey();
            String word2=(String)e2.getKey();
        } else {
                //Sort values in a descending order
                result=value2.compareTo( value1 );
            return result;

However the Values that get sorted are..
Value = 1a                         Key: 1
Value = 2a                         Key: 2
Value = da                         Key: 4
Value = 7a                         Key: 7
Value = 11a                         Key: 11
Value = 15a                         Key: 15
Value = BB23                         Key: 23
Value = 77a                         Key: 77
Value = AAA                         Key: 87
Value = aaa                         Key: 737

It seems to have sorted based upon the Keys?? But I'm not sure why it would be doing this?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros