Arrays.sort() ●6 Sorts the objects into ascending order,according to their natural ordering o This sort is guaranteed to be stable:equal elements will not be reordered as a result of the sort oYou can specify a range.The range to be sorted extends from index fromlndex,inclusive,to index tolndex,exclusive. oThe objects need to comparable or there is a special comparator
Arrays.sort() l Sorts the objects into ascending order, according to their natural ordering l This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort l You can specify a range. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. l The objects need to comparable or there is a special comparator
Arrays.sort()cont. sort(array),sort(array,fromlndex,tolndex) OAll elements in the array must implement the Comparable interface sort(array,comparator) sort(array,fromlndex,tolndex,comparator) oAll elements in the array must be mutually comparable by the specified comparator
Arrays.sort() cont. lsort(array), sort(array, fromIndex, toIndex) lAll elements in the array must implement the Comparable interface lsort(array, comparator) lsort(array, fromIndex, toIndex, comparator) lAll elements in the array must be mutually comparable by the specified comparator
Comparable interface oWith a single method compareTo() o Takes another Object as argument ●And returns: ONegative value if this is less than argument OZero value if this is equal to argument Opositive value if this is greater than argument
Comparable interface lWith a single method compareTo() lTakes another Object as argument lAnd returns: ¡Negative value if this is less than argument ¡Zero value if this is equal to argument ¡positive value if this is greater than argument
Comparator interface Two methods:compare(),equals() oOnly need to implement compare( o Takes two Object as argument: compare(Object o1,Object o2) ●And returns ONegative value if o1 is less than o2 OZero value if o1 is equal to o2 Opositive value if o1 is greater than o2
Comparator interface lTwo methods: compare(), equals() lOnly need to implement compare() lTakes two Object as argument: compare(Object o1, Object o2) lAnd returns ¡Negative value if o1 is less than o2 ¡Zero value if o1 is equal to o2 ¡positive value if o1 is greater than o2
Sort example:WorkerTest.java
Sort example: WorkerTest.java