Sorting Flashcards

1
Q

Give the signature of Collections.sort !

A

public static <T extends Comparable<? super T» void sort(List<T> list) :</T>

List<Integer> liste = new ArrayList<>();
liste.add(127);
liste.add(29);
liste.add(131);
Collections.sort(liste);
System.out.println(liste);//[29, 127, 131]</Integer>

List<String> liste = new ArrayList<>();
liste.add("C");
liste.add("B");
liste.add("A");
System.out.println(liste);// [C, B, A]
Collections.sort(liste);
System.out.println(liste);// [A, B, C]</String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain how Collections.sort sort an array of String with letters
and numbers !

A

UpperCase before LowerCase and Numbers before letters
EXAMPLE :

List<String> liste1 = new ArrayList<>();
liste1.add("a");
liste1.add("1");
liste1.add("Z");</String>

System.out.println(liste1);// [a, 1, Z]
Collections.sort(liste1);
System.out.println(liste1);// [1, Z, a]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly