`
Anybing
  • 浏览: 127319 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ArrayCompare

    博客分类:
  • Java
阅读更多

import java.util.Comparator;


/**
* Implement the Array customize Compare
* @author bevan
*
*/
public class ArrayCompare implements Comparator<Object> {
static int string = 0;
static int integer = 1;
static int asc = 1;

int[][] sortby = null;

/*
* Default construct
*/
public ArrayCompare() {

}


public ArrayCompare(int[][] sb) {
this.sortby= sb;
}

/* Implement the String Array compare
* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/

 
    public int compare(Object obj1, Object obj2)     {
    String[] sa1 = (String[])obj1;
    String[] sa2 = (String[])obj2;
    if(sortby == null ){ //Default compare
    int min = Math.min(sa1.length,sa2.length);
    for(int i = 0; i < min; i++) {  
    String s1 = sa1[i];
    String s2 = sa2[i];
    int result =  s1.compareTo(s2);
    if(result != 0){
    return result;
    }
    }
    return sa1.length - sa2.length;
    }else if(sortby != null){ //Customize Compare
    for(int i = 0; i < sortby.length;i++){
    int[] detail = sortby[i];
    int result = compareWithDataType(sa1[detail[0]],sa2[detail[0]],detail);
    if(result != 0) {
    return result;
    }
    }
    }
   
return 0;
   
    }
   
   
    public int compareWithDataType(String s1,String s2, int[] detail){
    int datatype = 0;
    int order = 1;
    if(detail.length > 1){
    datatype = detail[1];
    }
    if(detail.length > 2){
    order = detail[2];
    }
    if(datatype == string) {
    return s1.compareTo(s2) * order;
    }else if (datatype == integer){
    int i1 = new Integer(s1);
    int i2 = new Integer(s2);
    return (i1 - i2) * order;
    }
    return 0;
    }
}
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics