Sort ArrayValues Using Custom Comparator
Updated: Feb 28, 2026
This example demonstrates how to use the ArrayValue to store and sort a collection of Record objects using a custom approach.
It shows how to use custom Comparators to sort by different fields and criteria.
package com.northconcepts.datapipeline.examples.cookbook;
import com.northconcepts.datapipeline.core.ArrayValue;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.ValueNode;
import java.util.Comparator;
public class SortArrayValuesUsingCustomComparator {
public static void main(String[] args) {
ArrayValue employees = new ArrayValue();
employees.addValue(new Record()
.setField("id", 105)
.setField("firstName", "John")
.setField("lastName", "Smith")
.setField("salary", 75000.50)
.setField("active", true));
employees.addValue(new Record()
.setField("id", 102)
.setField("firstName", "Alice")
.setField("lastName", "Johnson")
.setField("salary", 85000.75)
.setField("active", true));
employees.addValue(new Record()
.setField("id", 108)
.setField("firstName", "Bob")
.setField("lastName", "Williams")
.setField("salary", 65000.25)
.setField("active", false));
employees.addValue(new Record()
.setField("id", 101)
.setField("firstName", "Carol")
.setField("lastName", "Davis")
.setField("salary", 92000.00)
.setField("active", true));
System.out.println("Before sort:");
printEmployees(employees);
// Sort by ID (integer field) in ascending order
System.out.println("\nSorting by ID (ascending):");
employees.sort(Comparator.comparing(node ->
((Record) node).getField("id").getValueAsInteger()
));
printEmployees(employees);
// Sort by last name (string field) alphabetically
System.out.println("\nSorting by Last Name (alphabetically):");
employees.sort(Comparator.comparing(node ->
((Record) node).getField("lastName").getValueAsString()
));
printEmployees(employees);
// Sort by salary (double field) in descending order
System.out.println("\nSorting by Salary (descending):");
employees.sort(Comparator.comparing((ValueNode> node) ->
((Record) node).getField("salary").getValueAsDouble()
).reversed());
printEmployees(employees);
// Sort by active status (boolean field), then by last name
System.out.println("\nSorting by Active status (active first), then by Last Name:");
employees.sort(
Comparator.comparing((ValueNode> node) ->
((Record) node).getField("active").getValueAsBoolean()
).reversed()
.thenComparing(node ->
((Record) node).getField("lastName").getValueAsString()
)
);
printEmployees(employees);
}
private static void printEmployees(ArrayValue employees) {
employees.forEach(node -> {
Record rec = (Record) node;
System.out.println(" ID: " + rec.getField("id").getValueAsInteger() +
", Name: " + rec.getField("firstName").getValueAsString() + " " + rec.getField("lastName").getValueAsString() +
", Salary: $" + rec.getField("salary").getValueAsDouble() +
", Active: " + rec.getField("active").getValueAsBoolean());
});
}
}
Code walkthrough
- An ArrayValue called employees is initialized and populated with four Record objects. Each Record represents an
employeeand contains fields forid,firstName,lastName,salary, andactivestatus. TheaddValuemethod is used to add each employee Record to the array, allowing us to later manipulate or sort the collection as needed. printEmployeesis used to output each employee's details (ID, name, salary, and active status) to the console. It displays current state of the employee list after sorting or modifying the array.- Next we sort the
employeesusing custom Comparators
Output
Before sort: ID: 105, Name: John Smith, Salary: $75000.5, Active: true ID: 102, Name: Alice Johnson, Salary: $85000.75, Active: true ID: 108, Name: Bob Williams, Salary: $65000.25, Active: false ID: 101, Name: Carol Davis, Salary: $92000.0, Active: true Sorting by ID (ascending): ID: 101, Name: Carol Davis, Salary: $92000.0, Active: true ID: 102, Name: Alice Johnson, Salary: $85000.75, Active: true ID: 105, Name: John Smith, Salary: $75000.5, Active: true ID: 108, Name: Bob Williams, Salary: $65000.25, Active: false Sorting by Last Name (alphabetically): ID: 101, Name: Carol Davis, Salary: $92000.0, Active: true ID: 102, Name: Alice Johnson, Salary: $85000.75, Active: true ID: 105, Name: John Smith, Salary: $75000.5, Active: true ID: 108, Name: Bob Williams, Salary: $65000.25, Active: false Sorting by Salary (descending): ID: 101, Name: Carol Davis, Salary: $92000.0, Active: true ID: 102, Name: Alice Johnson, Salary: $85000.75, Active: true ID: 105, Name: John Smith, Salary: $75000.5, Active: true ID: 108, Name: Bob Williams, Salary: $65000.25, Active: false Sorting by Active status (active first), then by Last Name: ID: 101, Name: Carol Davis, Salary: $92000.0, Active: true ID: 102, Name: Alice Johnson, Salary: $85000.75, Active: true ID: 105, Name: John Smith, Salary: $75000.5, Active: true ID: 108, Name: Bob Williams, Salary: $65000.25, Active: false
