All I keep seeing is sorting of string / integer array list. What I'm looking for how is how to sort a number. Example, I have a 190 format, I want it to sort to 019. Any help is appreciated, thanks!
7 Answers
You can do it like this:
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());
}
}
Output:
001345
Comments
You can create a List with the digits of your number and sort this List.
List<Integer> nums=new ArrayList<>();
for(int num=toSort;num>0;num/=10){
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){
result+=num.intValue();
/*int:
result+=num.intValue();
result×=10;*/
}
1 Comment
kwestionable
Hi there, I tried changing the
string result ="" to int and it shows the result of 10From my understanding of your question you are trying to sort the digits in an int. This can be done by converting to a CharArray, sorting, then returning the result. Note that this will fail for negative numbers.
fun sortInt(int: Int): Int =
int.toString()
.toCharArray()
.sorted()
.joinToString("")
.toInt()
EDIT: Now this will work for 0 digit also.
fun sortInt(int: Int): String =
int.toString()
.toCharArray()
.sorted()
.joinToString("")
Comments
Plenty of ways to achieve this. Here's one:
final String sorted = "190".chars()
.sorted()
.mapToObj(c -> Character.valueOf((char)c).toString())
.collect(Collectors.joining())
and
final String sorted = "190".chars()
.sorted()
.collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append)
.toString();
And, as a single-liner that will (I think) work in Java 7 (not production code but fun to do)...
final String sorted = Arrays.toString(new PriorityQueue<>(Arrays.asList("190".split("")))
.toArray(new String[0]))
.replaceAll("\\W+", "");
4 Comments
kwestionable
Your first answer works for me. Question, what does setting language level to 8 - Lambdas do?
PPartisan
It means you can access Java 8 features, like lambdas. Some features are supported on older versions of Android whilst some features are not.
kwestionable
Does using it affect functionalities of my old code?
PPartisan
Nope, it just adds new features
Updates @dan1st answer, to get desired result:
List<Integer> nums = new ArrayList<>();
String total = "";
for( int num = 190; num>0; num/=10 ){
nums.add(num%10);
}
Collections.sort(nums);
for (Integer x : nums) {
total += x.toString();
}
Integer finalResult = Integer.parseInt(total);
Log.i(TAG,"=== finalResult "+ finalResult);
O/P : 019
019as an int, it will be changed to19.742should be247019was just an example. I just need to sort every string/number in an ascending order.