java 根據任一對象的屬性,進行list排序的改進版

  1 package com.easycom.util;
  2 
  3 import java.lang.reflect.Field;
  4 import java.util.ArrayList;
  5 import java.util.Date;
  6 import java.util.List;
  7 
  8 import org.apache.poi.ss.formula.functions.T;
  9 
 10 public class CommonUtil<T> {
 11     //屬性的類型集合(基本類型)
 12     public static String GENERICTYPES[] = {"class java.lang.Integer", "int",
 13             "class java.lang.Double", "double", "class java.lang.Boolean", "boolean",
 14             "class java.lang.Float", "float", "class java.lang.Character", "char",
 15             "class java.lang.short", "short", "class java.lang.Byte", "byte",
 16             "class java.lang.Long", "long",
 17             "class java.lang.String", "class java.util.Date"};
 18     
 19     //根據對象屬性的名稱,而對list進行排序
 20     public List<T> sortByName( String sort, boolean orderBl, List<T> list){
 21         //檢查是否合法
 22         if ( list.size() <= 0 || sort == null || "".equals(sort) ) {
 23             return list;
 24         }
 25         
 26         //得到GENERICTYPES的指針
 27         int indexType = getMethodName( sort , list.get(0));
 28         if ( indexType < 0) {
 29             return list;
 30         }
 31         
 32         //開始排序
 33         List<T> newList = new ArrayList<T>();
 34         //收集移除list的位置指針
 35         int reIndex[] = new int[list.size()];
 36         int count = 0;
 37         try{
 38             T t;
 39             while(list.size()>0){
 40                 t = list.get(0);
 41                 Object oldValue = getFieldValue(t, sort);
 42                 int index = 0;
 43                 if(oldValue != null) {
 44                     for(int j = 1; j < list.size();j++){
 45                         Object newValue = getFieldValue(list.get(j), sort);
 46                         if(newValue == null){
 47                             t = list.get(j);
 48                             index = j;
 49                             break;
 50                         }
 51                         int temp  = compareAllType(oldValue, newValue, indexType);
 52                         if(orderBl){
 53                             if(temp > 0){
 54                                 t = list.get(j);
 55                                 oldValue = newValue;
 56                                 index = j;
 57                             }
 58                         }else{
 59                             if(temp < 0){
 60                                 t = list.get(j);
 61                                 oldValue = newValue;
 62                                 index = j;
 63                             }
 64                         }
 65                     }
 66                 }
 67                 newList.add(t);
 68                 reIndex[count++] = index;
 69                 list.remove(index);
 70             }
 71         }catch(Exception e){
 72             /*
 73              * 出錯不排序;
 74              * 還原list.
 75              */
 76             e.printStackTrace();
 77             for(count--; count >= 0; count--){
 78                 if(reIndex[count] >= list.size()) {
 79                     list.add(newList.get(count));
 80                 } else {
 81                     list.add(reIndex[count], newList.get(count));
 82                 }
 83                 newList.remove(count);
 84             }
 85             return list;
 86         }
 87         
 88         return newList;
 89     }
 90     
 91     //根據屬性名,得到對象的屬性的值
 92     public Object getFieldValue(T t, String name) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
 93         Field newField = t.getClass().getDeclaredField(name);
 94         newField.setAccessible(true);
 95         return (Object)newField.get(t);
 96     }
 97     
 98     //得到T對象中該屬性的類型GENERICTYPES的指針
 99     public int getMethodName( String name, T t){
100         int indexType = -1;
101         try {
102             Field flds = t.getClass().getDeclaredField(name);
103             String type = flds.getGenericType().toString();
104             for ( int i = 0; i < GENERICTYPES.length ; i++ ) {
105                 if ( GENERICTYPES[i].equals( type ) ){
106                     indexType = i;
107                     break;
108                 }
109             }
110         } catch (NoSuchFieldException e) {
111             return indexType;
112         } catch (SecurityException e) {
113             return indexType;
114         }
115         return indexType;
116     }
117     
118     public int compareAllType(Object oldValue, Object newValue, int indexType){
119         int temp = 0;
120         if( indexType < 16 ) {
121             indexType = indexType >>> 1;
122             switch (indexType) {
123             //int
124             case 0:
125                 temp = (int)oldValue-(int)newValue;
126                 break;
127             //double
128             case 1:
129                 if((double)oldValue>(double)newValue){
130                     temp = 1;
131                 }else if((double)oldValue>(double)newValue){
132                     temp = -1;
133                 }else{
134                     temp = 0;
135                 }
136                 break;
137             //boolean    
138             case 2:
139                 if((boolean)oldValue) {
140                     temp = 1;
141                 } else {
142                     temp = -1;
143                 }
144                 break;
145             //float    
146             case 3:
147                 if((float)oldValue>(float)newValue){
148                     temp = 1;
149                 }else if((float)oldValue>(float)newValue){
150                     temp = -1;
151                 }else{
152                     temp = 0;
153                 }
154                 break;
155             //char
156             case 4:
157                 if((char)oldValue>(char)newValue){
158                     temp = 1;
159                 }else if((char)oldValue>(char)newValue){
160                     temp = -1;
161                 }else{
162                     temp = 0;
163                 }
164                 break;
165             //short
166             case 5:
167                 if((short)oldValue>(short)newValue){
168                     temp = 1;
169                 }else if((short)oldValue>(short)newValue){
170                     temp = -1;
171                 }else{
172                     temp = 0;
173                 }
174                 break;
175             //byte
176             case 6:
177                 if((byte)oldValue>(byte)newValue){
178                     temp = 1;
179                 }else if((byte)oldValue>(byte)newValue){
180                     temp = -1;
181                 }else{
182                     temp = 0;
183                 }
184                 break;
185             //long
186             case 7:
187                 if((long)oldValue>(long)newValue){
188                     temp = 1;
189                 }else if((long)oldValue>(long)newValue){
190                     temp = -1;
191                 }else{
192                     temp = 0;
193                 }
194                 break;
195             //其餘狀況不排序
196             default:
197                 temp = 0;
198                 break;
199             }
200         } else {
201             switch (indexType) {
202             //String
203             case 16:
204                 temp = oldValue.toString().compareTo(newValue.toString());
205                 break;
206             //Date
207             case 17:
208                 Date oldDate = (Date)oldValue;
209                 Date newDate = (Date)newValue;
210                 if(oldDate.getTime() > newDate.getTime()){
211                     temp = 1;
212                 }else if(oldDate.getTime() < newDate.getTime()){
213                     temp = -1;
214                 }else{
215                     temp = 0;
216                 }
217                 break;
218             //其餘狀況不排序
219             default:
220                 temp = 0;
221                 break;
222             }
223         }
224         return temp;
225     }
226     
227     
228     
229     public static void main(String[] agrs){
230         int j = 10;
231         Object bObject = j;
232         System.out.println(bObject);
233     }
234     
235 }
相關文章
相關標籤/搜索