How to sort a property object wrt its values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    How to sort a property object wrt its values?

    Hello,

    I have a bit of code that prints the counts of objects in a list. i want to sort it by its count.

    code :
    Code:
    package util;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import org.apache.commons.collections.CollectionUtils;
    
    public class Count
    {
        public static void main(String args[]) throws Exception
        {
            ArrayList al = new ArrayList();
            al.add("a");
            al.add("a");
            al.add("a");
            al.add("a");
            al.add("a");
            al.add("b");
            al.add("b");
            al.add("c");
            al.add("d");
            
            System.out.println(al);
            printcounts(al);
        }
    
        private static void printcounts(ArrayList<String> al)
        {
            HashSet ht = new HashSet(al);
            Iterator<String> it = ht.iterator();
            while(it.hasNext())
            {
                String str = it.next();
                int count = CollectionUtils.cardinality(str, al);
    
                System.out.println("word : "+str+" count : "+count);
            }
        }
    }
    output :
    [a, a, a, a, a, b, b, c, d]
    word : d count : 1
    word : b count : 2
    word : c count : 1
    word : a count : 5


    i want to be,

    [a, a, a, a, a, b, b, c, d]
    word : a count : 5
    word : b count : 2
    word : d count : 1
    word : c count : 1

    How to do this?


    Thanks and regards
    jerald
  • pjerald
    New Member
    • Oct 2007
    • 77

    #2
    how to sort a property object wrt its values?

    i have done the code but with normal replacement sorting algorithm...

    Code:
    package util;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Properties;
    import org.apache.commons.collections.CollectionUtils;
    
    public class Count
    {
        public static void main(String args[]) throws Exception
        {
            ArrayList al = new ArrayList();
            al.add("a");
            al.add("a");
            al.add("a");
            al.add("a");
            al.add("a");
            al.add("b");
            al.add("b");
            al.add("c");
            al.add("d");
            
            System.out.println(al);
            printcounts(al);
    
            Properties hm = new Properties();
            hm.setProperty("a", "a");
            hm.setProperty("a", "a");
            hm.setProperty("a", "a");
            System.out.println(hm);
        }
    
        public static void printcounts(ArrayList<String> al)
        {
            HashSet ht = new HashSet(al);
            Iterator<String> it = ht.iterator();
            ArrayList<String> list1 = new ArrayList();
            ArrayList<Integer> list2 = new ArrayList<Integer>();
            while(it.hasNext())
            {
                String str = it.next();
                int count = CollectionUtils.cardinality(str, al);
                list1.add(str);
                list2.add(count);
            }
            System.out.println("List1 :"+list1);
            System.out.println("List2 :"+list2);
    
            for(int i=0;i<list2.size();i++)
            {
                Integer val = list2.get(i);
                String vals = list1.get(i);
                for(int j=0;j<i;j++)
                {
                    if(val.intValue() < list2.get(j))
                    {
                        int temp = list2.get(j);
                        list2.set(j, val);
                        list2.set(i, temp);
    
                        String temps = list1.get(j);
                        list1.set(j, vals);
                        list1.set(i, temps);
                    }
                }
            }
    
            //printing :
            for(int i=0;i<list2.size();i++)
            {
                System.out.println(list1.get(i)+" "+list2.get(i));
            }
        }
    }
    let me know if there is some API to do this?

    Comment

    Working...