Page 1 of 1

Interesting Thing About Sorting in Java

Posted: Mon Nov 05, 2012 12:21 am
by Raudel_UH
In Java when you use Arrays.sort() you are sorting with Quick Sort and when you use
Collections.sor() you are using Merge Sort so take out your own conclusions.

Re: Interesting Thing About Sorting in Java

Posted: Tue Jan 22, 2013 10:17 pm
by jelara
Thanks for the tip! I'm sure we'll all take this into account when solving our next exercise.

Re: Interesting Thing About Sorting in Java

Posted: Wed Jan 23, 2013 4:16 pm
by GoogleBot
Raudel_UH wrote:In Java when you use Arrays.sort() you are sorting with Quick Sort and when you use
Collections.sor() you are using Merge Sort so take out your own conclusions.
Arrays.sort(pa); Sorts the elements of the array of a primitive type into ascending order using their natural ordering.

Arrays.sort(pa, from, to); Sorts the elements pa[from]...pa[to-1] of a primitive type. into ascending order.

Arrays.sort(oa); Sorts the elements of the array of an object type into ascending order, using the order defined by Comparable interface, which defines the compareTo method. Note that many Java classes such as String (but not
StringBuffer), Double, BigInteger, etc implement Comparable.

Arrays.sort(oa, from, to); Sorts the elements of the array, in the range from...to of an object type into ascending order.

Arrays.sort(oa, comp); Sorts the elements of the array of an object type into ascending order, using the Comparator comp.

Arrays.sort(oa, from, to, comp); Sorts the elements of the array, in the range from...to of an object type into ascending order using the Comparator comp.

Re: Interesting Thing About Sorting in Java

Posted: Wed Jan 23, 2013 4:18 pm
by GoogleBot
Hola a todos, tengo el siguiente codigo y no logro entender el resultado luego de realizar el sort, como hace el sort() para ordenar?

El resultado del siguiente codigo es:

nickel button key lint OK, IMPRIMER TAL CUAL ESTA EL ARRAY.
nickel lint key button ESTO NO LO ENTIENDO, COMO SE ENCARGA DE ORDENAR ESTO??

Código:
package prueba.sort;


import java.util.Arrays;
import java.util.Comparator;

public class PRUEBASORT {

public static void main(String[] args) {

String [] sa = {"nickel","button","key","lint"};

Sorter s = new Sorter();

for(String s2 : sa) {
System.out.print(s2 + " ");
}

System.out.println("");

Arrays.sort(sa,s);

for(String s2 : sa) {
System.out.print(s2 + " ");
}
}

static class Sorter implements Comparator<String> {

@Override
public int compare(String a, String b) {

return b.compareTo(a);
}

}
}

Re: Interesting Thing About Sorting in Java

Posted: Sat Feb 02, 2013 11:55 am
by kangoonie
Hola a todos, tengo el siguiente codigo y no logro entender el resultado luego de realizar el sort, como hace el sort() para ordenar?
El codigo ordena segun la implementacion de la interface Comparator<String>, que esta implementada en la clase Sorter, y llama al metodo

Code: Select all

public int compare(String a, String b) {
return b.compareTo(a);
}
para comparar dos String. Dado que en el metodo se llama a

Code: Select all

return b.compareTo(a);
se invierte el ordenamiento. Para ordenar ascendentemente, utiliza

Code: Select all

return a.compareTo(b);