3 Sort
3.1 Sort List by another List
I’ll help you sort a list based on the order defined in a template list. This is a common pattern in programming where you want to maintain a specific ordering.
Here are several approaches to solve this problem:
3.1.1 Method 1: Using where()
and contains()
(Simple approach)
List<String> template = ["A", "B", "C", "D"];
List<String> x = ["D", "B"];
List<String> sortedX = template.where((item) => x.contains(item)).toList();
// Output: ["B", "D"] print(sortedX);
3.1.2 Method 2: Using sort()
with custom comparator
List<String> template = ["A", "B", "C", "D"];
List<String> x = ["D", "B"];
List<String> sortedX = List.from(x);
.sort((a, b) => template.indexOf(a).compareTo(template.indexOf(b)));
sortedX// Output: ["B", "D"] print(sortedX);
3.1.3 Method 3: Creating a reusable function
List<T> sortByTemplate<T>(List<T> template, List<T> items) {
return template.where((item) => items.contains(item)).toList();
}
void main() {
List<String> template = ["A", "B", "C", "D"];
List<String> x = ["D", "B"];
List<String> result = sortByTemplate(template, x);
// Output: ["B", "D"]
print(result); }
3.1.4 Method 4: More efficient approach using Set for larger lists
List<String> template = ["A", "B", "C", "D"];
List<String> x = ["D", "B"];
Set<String> xSet = x.toSet(); // Convert to Set for O(1) lookup
List<String> sortedX = template.where((item) => xSet.contains(item)).toList();
// Output: ["B", "D"] print(sortedX);
3.1.5 How it works:
The key concept is to iterate through the template list in order and filter out only the items that exist in your target list (x
). This preserves the original ordering from the template.
Visual representation:
Template: [A, B, C, D]
Target: [D, B]
Process:
A → not in target → skip
B → in target → include
C → not in target → skip
D → in target → include
Result: [B, D]