Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.TreeMap; import java.lang.reflect.*; import java.util.Map; public class Main { public static void sortObjectArrayListsSimple(ArrayList masterList, String paramName, ArrayList... listsIn) { int count = masterList.size(); for (ArrayList al : listsIn) { if (al.size() != count || count == 0) { System.out.println("counts of lists are not the same, did not sort"); return; } } ArrayList<ArrayList> result = sortObjectArrayListSimpleMaster(masterList, paramName); masterList.clear(); for (Object o : result.get(0)) masterList.add(o); //masterList = result.get(0); ArrayList[] orderedLists = new ArrayList[listsIn.length]; for (int i = 0; i < listsIn.length; i++) orderedLists[i] = new ArrayList(); for (int i = 0; i < result.get(1).size(); i++) { int index = (Integer) result.get(1).get(i); for (int j = 0; j < listsIn.length; j++) { orderedLists[j].add(listsIn[j].get(index)); } } for (int i = 0; i < listsIn.length; i++) { listsIn[i].clear(); for (int j = 0; j < orderedLists[i].size(); j++) { listsIn[i].add(orderedLists[i].get(j)); } } } private static ArrayList<ArrayList> sortObjectArrayListSimpleMaster(ArrayList listIn, String paramName) { ArrayList<ArrayList> answer = new ArrayList<ArrayList>(); ArrayList newList = new ArrayList(); ArrayList<Integer> indices = new ArrayList<Integer>(); try { if (listIn.size() > 0) { Class<?> c = listIn.get(0).getClass(); Field f = c.getDeclaredField(paramName); f.setAccessible(true); Class<?> t = f.getType(); Double dd = new Double(14); Float ff = new Float(14); Integer ii = new Integer(14); Map sortedPos = new LinkedHashMap(); Map sortedNeg = new LinkedHashMap(); Map unsorted = new LinkedHashMap(); int indexCount = 0; long count = 0; if (t.isPrimitive()) { for (Object thisObj : listIn) { Object o = f.get(thisObj); double d = 0; if (t.getName().equals("char")) { d = (int) ((Character) o); } else if (t.isInstance(dd)) d = (Double) o; else if (t.isInstance(ff)) d = (Float) o; else if (t.isInstance(ii)) d = (Integer) o; else d = new Double(o.toString()); boolean isNegative = false; if (d < 0) { isNegative = true; d = Math.abs(d); } String format = "%1$30f"; String newKey = String.format(format, d); String format2 = "%1$20d"; String countString = String.format(format2, count); newKey += "-" + countString; if (isNegative) { sortedNeg.put(newKey, thisObj); } else { sortedPos.put(newKey, thisObj); } unsorted.put(thisObj, indexCount); count++; indexCount++; } TreeMap<String, Object> resultPos = new TreeMap(); resultPos.putAll(sortedPos); sortedPos = resultPos; TreeMap<String, Object> resultNeg = new TreeMap(); resultNeg.putAll(sortedNeg); sortedNeg = resultNeg; } else if (t.isInstance(paramName)) { // System.out.println("is a string with value " + o); for (Object thisObj : listIn) { String key = (String) (f.get(thisObj)); sortedPos.put(key + "-" + count, thisObj); unsorted.put(thisObj, indexCount); count++; indexCount++; } TreeMap<String, Object> result = new TreeMap(String.CASE_INSENSITIVE_ORDER); result.putAll(sortedPos); sortedPos = result; } Iterator itNeg = sortedNeg.entrySet().iterator(); while (itNeg.hasNext()) { Map.Entry pairs = (Map.Entry) itNeg.next(); newList.add(pairs.getValue()); itNeg.remove(); } Collections.reverse(newList); Iterator itPos = sortedPos.entrySet().iterator(); while (itPos.hasNext()) { Map.Entry pairs = (Map.Entry) itPos.next(); Object obj = pairs.getValue(); newList.add(obj); indices.add((Integer) unsorted.get(obj)); itPos.remove(); } } } catch (Exception e) { System.out .println("problem sorting list. listIn.size(): " + listIn.size() + " and param: " + paramName); answer.add(newList); answer.add(indices); return answer; } answer.add(newList); answer.add(indices); return answer; } }