Strategy pattern for sorting key
Java
Hard
5 views
Problem Description
Task: implement strategy to pick sort key from object and sort list accordingly.
Output Format
In-place update
Constraints
Use functional interface.
Official Solution
static interface Key{int key(Node n);}static class Node{final int a;final int b;Node(int a,int b){this.a=a;this.b=b;}}static void sortBy(java.util.List<Node> list,Key k){list.sort((x,y)->Integer.compare(k.key(x),k.key(y)));}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!