Home > Enterprise >  How to sort an character array without using ASCII values? [closed]
How to sort an character array without using ASCII values? [closed]

Time:09-22

How To sort an character array without using ASCII Values?

I have seen many ways to sort an char array using ASCII values but i did'nt found any way to sort without using ASCII values.

CodePudding user response:

There are no functions in the standard libraries to sort an array of characters based on a Comparator, so, if you want to sort something like

char [] xs = ...

You must find or make your own implementation of the function

void sortCharArray(char [] xs, Comparator<Char> k);

or similar (i.e. using the quick sort algorithm).

If not, you must to convert your array to a boxed type and then, use the standard function

void sort(T[] a, Comparator<? super T> c)

the obvious conversion is

Character [] cs = new Character[xs.length];
for(int i = 0; i < xs.length; i  )
    cs[i] = Character.valueOf(xs[i]);

if you have a String you could use Streams

"abcABC".chars().boxed().sorted(yourComparator)...

CodePudding user response:

char charArray[]={'a', 'b', 'c', 'e', 'f', 'd', 'g'};
Arrays.sort(charArray);
System.out.println(Arrays.toString(charArray));
  • Related