Saturday, 26 May 2012

Index Sorting

Sorting Mechanism for Index

like "1" ,"1.1" , "1.2.1" and more


Sorting logic using Comparator


/**
     * this is only for integer type index
     * {"1", "1.1", "1.1.1", "1.10", "1.10.1", "1.10.2", "1.11", "1.2"}
     * kind of sorting
     */



private static Comparator<String> comparatorIndices = new Comparator<String>() {

            
        public int compare(String o1, String o2) {
            String[] c1 = o1.contains(".")?o1.split("\\."):new String[]{o1};
            String[] c2 = o2.contains(".")?o2.split("\\."):new String[]{o2};
          
            int c1l = c1.length;
            int c2l = c2.length;
          
            int length;
          
            if(c1l == c2l){
                length = c1l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return 0;
            }
          
            if(c1l < c2l){
                length = c1l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return -1;
            }
          
            if(c1l > c2l){
                length = c2l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return -1;
              
            }
            return 0;
        }
    };
   

You can also generate Tree using DefaultMutableTreeNod



public static void CreaateTree{

String[] str = new String[]{"1", "1.1", "1.1.1", "1.10", "1.10.1", "1.10.2", "1.11", "1.2", "1.2.1", "1.3", "1.3.1", "1.4", "1.4.1", "1.5", "1.5.1", "1.6", "1.6.1", "1.7", "1.7.1", "1.8", "1.8.1", "1.9", "1.9.1", "1.9.2", "2", "2.1", "2.1.1", "2.10", "2.10.1", "2.10.2", "2.11", "2.12", "2.13", "2.2", "2.2.1", "2.2.1.1", "2.2.1.1.1", "2.2.1.1.1.1", "2.2.1.2", "2.3", "2.3.1", "2.4", "2.4.1", "2.5", "2.5.1", "2.6", "2.6.1", "2.7", "2.7.1", "2.8", "2.8.1", "2.9", "2.9.1", "2.9.2", "3", "3.1", "3.1.1"};
        

Collections.sort(Arrays.asList(str), comparatorIndices);
       
        DefaultTreeModel model;
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("0",true);
        model = new DefaultTreeModel(top);
       
        for(String string:str){
            DefaultMutableTreeNode findTreeNode = findTreeNode(getParentId(string));
            if(null != findTreeNode){
                top = findTreeNode;
                top.add(new DefaultMutableTreeNode(string,true));
            }else{
                top.add(new DefaultMutableTreeNode(string,true));
            }
        }
}



public static String getParentId(String child){
        if(null != child && !"".equalsIgnoreCase(child)){
            if(-1 != child.lastIndexOf(".")){
                return child.substring(0, child.lastIndexOf("."));
            }else{
                return "0";
            }
        }
        return "";
    }
   
    public static DefaultMutableTreeNode findTreeNode(String nodeId) {
        DefaultMutableTreeNode rootNode =
                (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node;
        String tmp;
        Enumeration nodes = rootNode.depthFirstEnumeration();
        while (nodes.hasMoreElements()) {
            node = (DefaultMutableTreeNode) nodes.nextElement();
            tmp = (String) node.getUserObject();
            if (nodeId.equals(String.valueOf(tmp))) {
                return node;
            }
        }
        return null;
    }





No comments:

Post a Comment

Infinidb _CpNoTf_ problem

infinidb table with a varchar column insert string as a '_CpNoTf_' while using Cpimport. The Problem is occured if inserted string ...