Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java array sorting java8

//1. Natural ordering

User[] sortedUserArray = Stream.of(userArray)
            .sorted()
            .toArray(User[]::new);

//2. Reverse ordering

User[] sortedUserArray = Stream.of(userArray)
            .sorted(Comparator.reverseOrder())
            .toArray(User[]::new);

//3. Custom Sorting

Comparator nameComparator = Comparator.comparing(Employee::getName)
	.thenComparing(Employee::getId)

User[] sortedUserArray = Stream.of(userArray)
            .sorted(nameComparator)
            .toArray(User[]::new);
Source by howtodoinjava.com #
 
PREVIOUS NEXT
Tagged: #java #array #sorting
ADD COMMENT
Topic
Name
2+6 =