如何在java android中对数字进行排序?

我一直看到的是字符串/整数数组列表的排序。我正在寻找的是如何对数字进行排序。例如,我有一个190格式,我希望它排序为019.任何帮助表示赞赏,谢谢!



手掌心
浏览 156回答 7
7回答

哔哔one

你可以这样做:   package test;    public class Test2 {        public static void main(String[] args) throws Exception {            String number = "153004";            String sorted = sortStringNumbers(number);            System.out.println(sorted);        }        private static String sortStringNumbers(String numbersString) {            return numbersString.chars().mapToObj(c -> Character.toString((char)c)).sorted().collect(Collectors.joining());        }    }输出:001345

慕的地6264312

您可以将您的 int 转换为一个 String 并拆分成一个数组,接下来只需对流进行排序并加入一个 String。String[] number = "190".split("");String list =  Stream.of(number).sorted().collect(Collectors.joining());System.out.println(list); //Output: 019

长风秋雁

您可以使用号码的数字创建一个列表并对该列表进行排序。List<Integer> nums=new ArrayList<>();for(int num=toSort;num>0;num/=10){&nbsp; &nbsp; nums.add(num%10);}Collections.sort(nums);String result="";//If you want the result to be a integer (e.g. 190 will convert into 19 and not 019), change this to int result=0;for(Integer num:nums){&nbsp; &nbsp; result+=num.intValue();&nbsp; &nbsp; /*int:&nbsp; &nbsp; result+=num.intValue();&nbsp; &nbsp; result×=10;*/}

炎炎设计

根据我对您问题的理解,您正在尝试对 int 中的数字进行排序。这可以通过转换为 CharArray、排序然后返回结果来完成。请注意,这对于负数将失败。fun&nbsp;sortInt(int:&nbsp;Int):&nbsp;Int&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int.toString() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toCharArray() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.sorted() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.joinToString("") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toInt()编辑:现在这也适用于 0 位数字。fun&nbsp;sortInt(int:&nbsp;Int):&nbsp;String&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int.toString() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toCharArray() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.sorted() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.joinToString("")

蓝山帝景

有很多方法可以实现这一目标。这是一个:final String sorted = "190".chars()&nbsp; &nbsp; .sorted()&nbsp; &nbsp; .mapToObj(c -> Character.valueOf((char)c).toString())&nbsp; &nbsp; .collect(Collectors.joining())和final String sorted = "190".chars()&nbsp; &nbsp; .sorted()&nbsp; &nbsp; .collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append)&nbsp; &nbsp; .toString();而且,作为将(我认为)在 Java 7 中工作的单行代码(不是生产代码,但很有趣)...final String sorted = Arrays.toString(new PriorityQueue<>(Arrays.asList("190".split("")))&nbsp; &nbsp; .toArray(new String[0]))&nbsp; &nbsp; .replaceAll("\\W+", "");

潇湘沐

&nbsp; &nbsp;List<Integer> nums = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; String total = "";&nbsp; &nbsp; &nbsp; &nbsp; for( int num = 190; num>0; num/=10 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nums.add(num%10);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(nums);&nbsp; &nbsp; &nbsp; &nbsp; for (Integer x : nums) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += x.toString();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Integer finalResult = Integer.parseInt(total);&nbsp; &nbsp; &nbsp; &nbsp; Log.i(TAG,"=== finalResult&nbsp; "+ finalResult);&nbsp; &nbsp; &nbsp; &nbsp; O/P : 019

胡子哥哥

如果你使用这个,你应该小心:sortInt(190)fun&nbsp;sortInt(int:&nbsp;Int):&nbsp;Int&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int.toString() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toCharArray() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.sorted() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.joinToString("") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toInt()输出将是 19 而不是 019
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java