Dada una ArrayList sin clasificar, la tarea es ordenar esta ArrayList en orden ascendente en Java.
Ejemplos:
Aporte : ArrayList sin clasificar: [Geeks, For, ForGeeks, GeeksForGeeks, un portal informático]
Producción : Sorted ArrayList: [Un portal informático, For, ForGeeks, Geeks, GeeksForGeeks]
Aporte : ArrayList sin clasificar: [Geeks, For, ForGeeks]
Producción : Lista de matrices ordenada: [Para, ParaGeeks, Geeks]cuantas ciudades hay en estados unidos
Acercarse: Una ArrayList se puede ordenar utilizando el método sort() de la clase Colecciones en Java. Este método sort() toma la colección que se va a ordenar como parámetro y devuelve una colección ordenada en orden ascendente de forma predeterminada.
Sintaxis:
Collections.sort(ArrayList);>
A continuación se muestra la implementación del enfoque anterior:
películas indias
// Java program to demonstrate> // How to sort ArrayList in ascending order> > import> java.util.*;> > public> class> GFG {> >public> static> void> main(String args[])> >{> > >// Get the ArrayList> >ArrayList> >list =>new> ArrayList();> > >// Populate the ArrayList> >list.add(>'Geeks'>);> >list.add(>'For'>);> >list.add(>'ForGeeks'>);> >list.add(>'GeeksForGeeks'>);> >list.add(>'A computer portal'>);> > >// Print the unsorted ArrayList> >System.out.println(>'Unsorted ArrayList: '> >+ list);> > >// Sorting ArrayList in ascending Order> >// using Collection.sort() method> >Collections.sort(list);> > >// Print the sorted ArrayList> >System.out.println(>'Sorted ArrayList '> >+>'in Ascending order : '> >+ list);> >}> }> |
tamaño del vector c ++
>
>Producción:
Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Sorted ArrayList in Ascending order : [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]>