java 将字符串转换为字符并按降序排序(ascii)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12800698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:16:42  来源:igfitidea点击:

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

javastringchar

提问by kix

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.

我正在创建一个程序,它将使用户输入整数(一个接一个),存储在数组中并按降序显示整数。该程序还要求用户输入一个字符串,使用string.toCharArray(). 我已经正确地将整数显示为降序。问题是我不知道如何降序显示字符。

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.

顺便说一句,我使用 flag 作为临时数组存储。

采纳答案by Peter Lawrey

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

回答by Rohit Jain

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 Comparatorand pass that to Arrays.sort()method..

对于降序,您可以定义 aComparator并将其传递给Arrays.sort()方法..

You can use Arrays#sortand ArrayUtils#toObjectfor sorting in Descending order..

您可以使用Arrays#sortArrayUtils#toObjectDescending 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);
    }