Skip to content

Instantly share code, notes, and snippets.

@ashikuzzaman-ar
Last active April 11, 2021 06:31
Show Gist options
  • Save ashikuzzaman-ar/ed05b2e3ec2d1ae1aedbf01e380c6d9b to your computer and use it in GitHub Desktop.
Save ashikuzzaman-ar/ed05b2e3ec2d1ae1aedbf01e380c6d9b to your computer and use it in GitHub Desktop.
Question: you have an array of string and you have to sort them according to their size. Example: Input: ["ab", "a", "abc", "z", "abcd", "mp"]; Output: ["a", "z", "ab", "mp", "abc", "abcd"]
/*
Question: you have an array of string and you have to sort them according to their size.
Example:
Input: ["ab", "a", "abc", "z", "abcd", "mp"];
Output: ["a", "z", "ab", "mp", "abc", "abcd"]
*/
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Solution {
public static void main(String[] args) {
List<String> input = List.of("ab", "a", "abc", "z", "abcd", "mp");
System.out.println(input.stream().sorted(Comparator.comparingInt(String::length)).collect(Collectors.toList()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment