Sort the character arrays insensitive in java

Please tell me - How sort character array case insensitive?

input: World

Output: dlorW

Algorithm is as follows:

  1. extract all lowercase and uppercase letters into different string

  2. sort each string differently

  3. append the sorted string

  4. display the result

  5. 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.

SEE IMPELMENTATION OF CODE

@neilit1992 Were you using MergeSort. I don’t find it easy.

May be its bad practice a but i think the dummy way will be.

  1. Create 2 new array 1 to store index of characters in actual array and other for converted upercase or Lower case array.
  2. sort the converted array along with the index relocation
  3. and then use index to create sorted result of old array

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

1 Like

10W8TD - Online C++0x Compiler & Debugging Tool - Ideone.com it is the link to the way vijju stated

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 ? :expressionless:

Can you check it in ideone.com