Sort ArrayList with sort() | Sololearn: Learn to code for FREE!

+1

Sort ArrayList with sort()

Hi i want sort an arraylist object as: Character ch=new character[al.size()]; Arrays.sort(al); But i got connot find symbol error But with Collections.sort(al); It do ok

7/3/2019 12:18:06 PM

Hadi

12 Answers

New Answer

+1

Hadi I know that, but is ArrayList<Character> object convertable to Object[ ]? NO Convert to array first then sort. Working code https://code.sololearn.com/cFG5LdVV1ZIW/?ref=app

+3

Hadi Check if you are not forgetting to import Character and Arrays classes. Also check if there is a typing error between uppercase I and l (lowercase L) If this does not help then please share the code.

+1

Hadi The Character class is in java.lang.Character package, you haven't imported the package.

+1

Hadi Can you tell what error you are getting? I mean which symbol is not found.

+1

Hadi In the code you have posted in your question you have Character ca=new character[al.size()]; i.e character and no array and in the latest code you have Character[] ca=new Character[al.size()]; Just to confirm, whether the first code has typing error or it is like that only.

+1

~ swim ~ Ok thank you And Arrays for static arrays but Collections for daynamic arrays

0

i import it as import java.util.*; my method: public static void sortArr(ArrayList<Character> al) { Character[] ca=new Character[al.size()]; Arrays.sort(al); al.toArray(ca); System.out.println("\n\nList Of Item Array: "); for(char c:ca) System.out.print(c+" "); }

0

Java.lang is default imported in java But after import lang i got error again: ArraySort.java:23: cannot find symbol symbol : method sort(java.util.ArrayList<java.lang.Character>) location: class java.util.Arrays Arrays.sort(al);

0

~ swim ~ Last my code( in method) has that error

0

Hadi None of the Arrays.sort method takes ArrayList or it base class as parameter. Check the syntax of Arrays.sort method. You can try this. Convert the ArrayList object to Array, and then sort that array. Currently you are trying to sort first and then converting to array.

0

Syntax of sort: void sort(Object[] a)            Sorts the specified array of objects into ascending order, according to the natural ordering of its elements

0

Hadi Yes you can say that.