char[] tmpArr = T.toCharArray();
Here is the mistake, you can't use premitives with comparator.
You need to convert it to Character[].
You could do something like below with Apache commons-lang:
char[] charArray = str.toCharArray();
Character[] tmpArr = ArrayUtils.toObject(charArray);
Here are some good ways to convert.
Answer from Krupal Shah on Stack Overflowchar[] tmpArr = T.toCharArray();
Here is the mistake, you can't use premitives with comparator.
You need to convert it to Character[].
You could do something like below with Apache commons-lang:
char[] charArray = str.toCharArray();
Character[] tmpArr = ArrayUtils.toObject(charArray);
Here are some good ways to convert.
It looks like there is no auto unboxing to Character.
To be precise, primitive to object conversion at compile time is called boxing. Unboxing is the reverse way.
And yes Character/char provides an autoboxing feature but it doesn't work in this way. Here the primitives/wrapper counterparts are in an array or a list : so no boxing possible here.
That compiles :
char a = Character.valueOf('a'); // compile
Character aa = 'a'; // compile
But that does not :
List<Character> charList = ...;
char[] charArray= ...;
charList = charArray; // doesn't compile
charArray = charList; // doesn't compile
How can I make this work?
Sorting an array of char with a specific Comparator is not conventional.
So not surprising that the Arrays API doesn't provide such a method.
Generally, Comparator and Comparable implementations rely only on the state of the compared objects to return the result, not on an external variable as in your example.
Similarly the Arrays API doesn't provide any method to create a Stream of char because streaming characters is also not a very conventional need.
So you don't have a straight built-in way to solve your issue.
In your situation, I would rely onArrays.stream(String.split()) to get a String for each character of the original String and I would apply the sort and finish with a joining :
String originalValue = "...";
String afterSortValue =
Arrays.stream(originalValue.split(""))
.sorted((o1, o2) -> {
Integer aPos = poses.get(o1);
Integer bPos = poses.get(o2);
if (aPos == null || bPos == null)
return 0;
return Integer.compare(aPos, bPos);
}
)
.collect(Collectors.joining());
This should be the most direct way without using external libraries.
Sorting a char array in java - Stack Overflow
How can you manually sort a char array?
java - Sorting characters alphabetically in a String - Stack Overflow
sorting - Sort character array using java comparator - Stack Overflow
characters can be directly compared as the comparison operators are overloaded. w>q i.e
arr[1]>arr[0]
is valid and gives true. So just treat them like integers in the bubble sort algorithm.
Using bubble sort or something?
Yes, a bubble sort would work. Or you could use any one of the other well-known sort algorithms; e.g. as listed in this Wikipedia page ... or any good textbook on data structures and algorithms.
(But no, I'm not going to code this for you.)
Without the use of Arrays.sort(). I just want to know how you'd do it, I think it would be helpful to know.
So like if someone inputs a String "ABBCAC" then I can sort that into AABBCC and maybe count the number of times it takes to sort it?
You may do the following thing -
1. Convert your String to char[] array.
2. Using Arrays.sort() sort your char array
Code snippet:
String input = "hello";
char[] charArray = input.toCharArray();
Arrays.sort(charArray);
String sortedString = new String(charArray);
System.out.println(sortedString);
Or if you want to sort the array using for loop (for learning purpose) you may use (But I think the first one is best option ) the following code snippet-
input="hello";
char[] charArray = input.toCharArray();
length = charArray.length();
for(int i=0;i<length;i++){
for(int j=i+1;j<length;j++){
if (charArray[j] < charArray[i]) {
char temp = charArray[i];
charArray[i]=arr[j];
charArray[j]=temp;
}
}
}
You can sort a String in Java 8 using Stream as below:
String sortedString =
Stream.of("hello".split(""))
.sorted()
.collect(Collectors.joining());
The problem is that if passing 'c' and 'b' makes the comparison return a negative value, then passing 'b' and 'c' should return a positive one (And your first version returns a negative number in that case instead). If the function doesn't return consistent results no matter the order of arguments, the sort algorithm is going to produce a garbage order.
Consider this version:
public int compare(Character x, Character y) {
if (x == 'c' && y == 'b') {
return -1;
} else if (x == 'b' && y == 'c') {
return 1;
} else {
return x.compareTo(y);
}
}
You can add System.out.println() to understand how it work:
Code
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
String s = "abracadabra";
int n = s.length();
System.out.println("X Y\tX Y\t[if] VALUE");
System.out.println();
Character[] res = new Character[n];
for (int i = 0; i < n; i++) {
res[i] = s.charAt(i);
}
int min = 'a';
Arrays.sort(res, new Comparator<Character>() {
@Override
public int compare(Character x, Character y) {
System.out.print(y + " " + x + "\t" + (x-min) + " " + (y-min) + "\t");
if(x == 'c' && y == 'b') {
System.out.println("true " + (y - x));
return y - x;
}
System.out.println(" " + (x - y));
return x - y;
}
});
System.out.println("#################################\nResult:\n" + Arrays.toString(res));
}
}
Console:
X Y X Y [if] VALUE
a b 1 0 1
b r 17 1 16
r a 0 17 -17
b a 0 1 -1
a a 0 0 0
b c 2 1 true -1
a c 2 0 2
c a 0 2 -2
a a 0 0 0
c d 3 2 1
r d 3 17 -14
b d 3 1 2
c a 0 2 -2
a a 0 0 0
a a 0 0 0
c b 1 2 -1
a b 1 0 1
a b 1 0 1
b r 17 1 16
d r 17 3 14
r r 17 17 0
c a 0 2 -2
a a 0 0 0
b a 0 1 -1
a a 0 0 0
#################################
Result:
[a, a, a, a, a, b, c, b, d, r, r]
Any tips towards sorting an array of character's into alphabetical order. I have all of the input I'm just having trouble with the method to sort it
I convert to an array of strings, sort it ignoring case then convert it back to an array of characters.
public char[] sortArray(char[] arr) {
// Creating a blank string array
String [] end = new String[arr.length];
// Converting (char) arr array to (string) end array.
for(int i = 0; i < arr.length; i++) {
end[i] = "" + arr[i];
}
// Sorts the end array ignoring case
Arrays.sort(end, String.CASE_INSENSITIVE_ORDER);
// Converting (string) end array to (char) arr array.
for(int i = 0; i < arr.length; i++) {
arr[i] = end[i].charAt(0);
}
// Returns arr aray
return arr;
}
Much cleaner way of doing it :
Java 8 +
Arrays.sort(characters, (c1, c2) -> Character.toLowerCase(c1) - Character.toLowerCase(c2));
Java 7 and below
Arrays.sort(characters, new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
return Character.toLowerCase(c1) - Character.toLowerCase(c2);
}
});