Please tell me - How sort character array case insensitive?
input: World
Output: dlorW
Please tell me - How sort character array case insensitive?
input: World
Output: dlorW
Algorithm is as follows:
extract all lowercase and uppercase letters into different string
sort each string differently
append the sorted string
display the result
If you need code, comment
One Thing you can do is to either convert all the uppercase to lower case or vice versa and mark them to be changed later.
After doing the above operation just sort the string as usual.
After sorting, traverse the string and convert all those characters stored in temporary vector say mv to be changed later to Uppercase. (Considering you had earlier converted uppercase to lowercase )
Print Your result.
May be its bad practice a but i think the dummy way will be.
As you are looking for a case insensitive sort, it is like a custom sort where you can use a Comparator.
You need to convert the String to char[].
Pass it to Arrays.sort(array,comparator);
Please check if this works.
Here is the code
public class ArrayCheck {
public static void main(String[] args) {
String str="World";
char[] charArray = str.toCharArray();
Character[] temp=new Character[charArray.length];
for(int pos=0;pos<charArray.length;pos++){
temp[pos]=charArray[pos];
}
CaseInsesitive caseI=new CaseInsesitive();
Arrays.sort(temp,caseI);
System.out.println(Arrays.toString(temp));
}
}
class CaseInsesitive implements Comparator{
@Override
public int compare(Character o1, Character o2) {
return String.valueOf(o1).toLowerCase().compareTo(String.valueOf(02).toLowerCase());
}
}
Any constraints? Which complexity algo you want? O(N^2)? O(NlogN)?
No constraints.
By Append, do you mean adding at end or mixing them? Because I feel that for a string “bAdC” he wants it as “AbCd”. Meaning alphabets sorted irrespective of case.
does he want bdAC or as you stated, AbCd? He doesn’t explain that. :
If it is like yours I can use the concept of merge sort while appending sorted strings. Yup appendingg means adding at the end.
I feel he wanted AbCd. I think you should also provide that solution (who knows, might help some1 else XD)
is it possible to use sort function?
yup u can use sort function
check my implementation it is simpler!
I wasn’t using merge sort though it can be solved using merge sort, check the code probably you’ll get it. I just modified the sort function.
I cant find your code ?