Converting string to char and sort in descending order (ascii)

24,740

Solution 1

You can try

String str = "AbCdEf";
char[] arr = str.toCharArray();

for(int i = 0; i < arr.length; i++) System.out.print(arr[i]);
System.out.println();

Arrays.sort(arr); // sorted in ascending order

// print them backwards i.e. descending order.
for(int i = arr.length - 1; i >= 0; i--) System.out.print(arr[i]);
System.out.println();

prints

AbCdEf
fdbECA

Solution 2

You already have built-in method for that: -

    String str = "Rohit";
    char[] arr = str.toCharArray();

    System.out.println(arr);
    Arrays.sort(arr);   // Sort in Ascending order
    System.out.println(arr);

For descending order, you can define a Comparator and pass that to Arrays.sort() method..

You can use Arrays#sort and ArrayUtils#toObject for sorting in Descending order..

Here's how it works: -

    String str = "Rohit";
    char[] charArray = str.toCharArray();

    Character[] myCharArr = ArrayUtils.toObject(charArray);

    Arrays.sort(myCharArr, new Comparator<Character>() {

        @Override
        public int compare(Character char1, Character char2) {
            return char2.compareTo(char1);
        }
    });

    for (char val: myCharArr) {
        System.out.print(val);
    }
Share:
24,740
kix
Author by

kix

Updated on July 09, 2022

Comments

  • kix
    kix almost 2 years

    I am creating a program which will make the user input integer (one after another), stored in array and display the integers in descending order. The program also ask to the user to input a string convert it to char using string.toCharArray(). I've correctly done displaying the integer into descending order. The problem is I don't know how to display the char descendingly.

    This is the code for the integer:

    for(i=0;i<nums.length;i++){
            System.out.print("Enter value for index["+i+"] here --> ");
            nums[i]=Integer.parseInt(br.readLine());}
    
    while(pass<nums.length){
            for(i=0;i<(nums.length-pass);i++){
                if(nums[i]<nums[i+1]){
                    temp=nums[i];
                    nums[i]=nums[i+1];
                    nums[i+1]=temp;
                }
            }
            pass++;
    }
    System.out.println("\n\nThe numbers when sorted descendingly are :\n");
        for(i=0;i<nums.length;i++){
            System.out.println(nums[i]);
    

    This is the code for the string to array. This is where i'm having problems. I don't have errors in running the program just it's just I don't how to do it correctly.

    System.out.print("Input a string");
            String strValue= br.readLine();
            char[] chrValues;
            chrValues=strValue.toCharArray( );
    
    while(flag<chrValues.length){
       for (c=0; c< (chrValues.length- flag); c++){
            if(chrValues[c]<chrValues[i+1]){
             tempo=chrValues[c];
                    chrValues[c]=chrValues[c+1];
                    chrValues[c+1]= tempo;}
       }
    }
     flag++; 
     System.out.println("\n\nThe String when converted in character and sorted descendingly     are :\n");    
        for(c=0;i<chrValues.length;c++){
            System.out.println(chrValues[c]);}
    

    By the way, I used flag as a temporary array storage.

  • kix
    kix over 11 years
    Hi Rohit! thanks for the response. Can i use the If statement in sorting? because "a" is different in "A" in ascii code. Thanks
  • Rohit Jain
    Rohit Jain over 11 years
    @kix.. So, you want them to be considered same??
  • Vishy
    Vishy over 11 years
    You can sort them is ascending order and print them backwards. ;)
  • Rohit Jain
    Rohit Jain over 11 years
    @kix.. Or you can convert both of them to lowercase, before invoking compareTo ... Edited code, accordingly..
  • Rohit Jain
    Rohit Jain over 11 years
    @PeterLawrey.. +1 Well, sometimes, we SHOULD use Traditional for loop.. No need to sort backwards.. Great :)
  • kix
    kix over 11 years
    @rohit nope. i want them to considered different. example i'll input "Abca" the output should be: a c b A. btw, we are not allowed to change them to lowercase :(
  • kix
    kix over 11 years
    @Rohit - i dont know if it's reverse but the answer go in like that because of the ascii table. anyways thanks for your help man. You're so good.
  • kix
    kix over 11 years
    @peter - thanks man! will try this and give you a feedback later.
  • kix
    kix over 11 years
    @rohit - sorry man. but im new here and im looking where i could "like" or maybe add "+1" to your answer and it seems that its the "accept" button. my bad.
  • kix
    kix over 11 years
    @peter - why am i having a possible loss of precision required: char found int on this code `for(int i = arr.length - 1; i >= 0; i--) System.out.print(arr[i]); System.out.println();
  • Vishy
    Vishy over 11 years
    @kix You have a problem in your copy because it compiles and runs for me.
  • Rohit Jain
    Rohit Jain over 11 years
    @kix.. Its ok.. now you know it.. Also, you can add +1 to any answer by voting up the answer.. Just click on the up-arrow, you see where you accepted the answer..