J'ai un problème avec le comparator java : apres le traitement ma classe est non trié ; voila ma méthode :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
public static GTCollaborateur[] getTriCollaborateurs(
			GTCollaborateur[] liste, String field) {
		try {
		 	Arrays.sort(liste, new GTCollaborateurComparator(field));
		} catch (Throwable t) {
			t.printStackTrace();
			common.log.errLog("Erreur tri collaborateurs", t);
		}
		return liste;
	}
et voila mon comparateur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class GTCollaborateurComparator implements Comparator {
 
	private Field fieldToCompare;
	private Object objInstance;
 
	public GTCollaborateurComparator(String field) throws SecurityException,
			NoSuchFieldException, InstantiationException,
			IllegalAccessException {
		fieldToCompare = GTCollaborateur.class.getDeclaredField(field);
		objInstance = fieldToCompare.getType().newInstance();
	}
 
 
	public int compare(Object o1, Object o2) {
		try {
 
			String methodName = String.valueOf(fieldToCompare.getName().charAt(
					0));
			methodName = methodName.toUpperCase();
			methodName += fieldToCompare.getName().substring(1);
			methodName = "get" + methodName;
			Method method = GTCollaborateur.class.getMethod(methodName, null);
 
			Object fieldValue1 = method.invoke(o1, null);
			Object fieldValue2 = method.invoke(o2, null);
 
			 if (objInstance instanceof String) {
				if (((String) fieldValue1).compareTo((String) fieldValue2) < 0)
					return -1;
				else if (((String) fieldValue1).compareTo((String) fieldValue2) > 0)
					return 1;
				else
					return 0;
 
			}    	 
 
		} catch (Throwable t) {
			System.err.println(t);
		}
 
		return 0;
	}


le traitement passe sans faute (sans bug ) mais en fin de compte ma liste n'est pas trié !!

Si qlq un a une solution , merci de me répondre
Merci a vous