diff --git a/tp/src/main/java/org/example/Main.java b/tp/src/main/java/org/example/Main.java index 4eeab4cc64b99908a783bcfff7d0e5a3d5891ada..d82ac5a22153c8eba43c4edb1e5455effac746c7 100644 --- a/tp/src/main/java/org/example/Main.java +++ b/tp/src/main/java/org/example/Main.java @@ -182,17 +182,23 @@ public class Main { } public static void OrderArray(){ int size_array = contactArray.length; - Contact[] newContactArray = new Contact[size_array]; - for(int i = 0; i < size_array -1; i++){ - int test = contactArray[i].getPers().getFirst_name().compareTo(contactArray[i + 1].getPers().getFirst_name()); - if(test < 0){ - newContactArray[i] = contactArray[i+1]; - newContactArray[i+1] = contactArray[i]; - } - else{ - newContactArray[i] = contactArray[i]; + if(size_array > 1){ + Contact[] newContactArray = new Contact[size_array]; + for(int i = 0; i < size_array - 1; i++){ //-1 as we check +1 later + String nameP1 = contactArray[i].getPers().getFirst_name(); + String nameP2 = contactArray[i+1].getPers().getFirst_name(); + int test = nameP1.compareTo(nameP2); //Return distance between 2 letter (f and a = -5 / a and b = 1) + if(test > 0){ + newContactArray[i] = contactArray[i+1]; + newContactArray[i+1] = contactArray[i]; + } + else{ + newContactArray[i] = contactArray[i]; + } + if(i == size_array - 2){ //-2 to take last iteration only (as we do "i < size -1") + contactArray = newContactArray; + } } } - contactArray = newContactArray; } } \ No newline at end of file diff --git a/tp/target/classes/org/example/Main.class b/tp/target/classes/org/example/Main.class index 3aff0a415e64adc1a9b945267ae353fc19a8e01e..611c4014bad05fc0a784690238cdc1e343376535 100644 Binary files a/tp/target/classes/org/example/Main.class and b/tp/target/classes/org/example/Main.class differ diff --git a/tp/target/classes/org/example/Person.class b/tp/target/classes/org/example/Person.class index 9b0cf630b4ca5daa47bd697933fc04b89efe9afc..3d13150b37dbed3bd7c91f402110ca11a8bf6268 100644 Binary files a/tp/target/classes/org/example/Person.class and b/tp/target/classes/org/example/Person.class differ