Sort & Dedupe Lines: how it works and how to build one
Sort, remove duplicates, trim, reverse and shuffle lines. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Clean up lists of emails, SKUs, keywords or log lines. Combine options like trim, remove empty lines and remove duplicates, then sort A to Z, by length or numerically.
When to use it
- Cleaning email lists
- Sorting keywords
- Removing duplicate log lines
How to use it
- Paste a list or open a file.
- Pick the clean-up options and a sort order.
- Copy or download the cleaned list.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/line-tools
How it works
Each option is a simple array step: split the text on new lines, trim each line, filter out empty lines, remove duplicates with a Set, sort with localeCompare (or numerically), then join again.
Ignoring case for duplicates means comparing a lower-cased key while keeping the first original line.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
lines.ts (ts)
export function cleanLines(text: string, { trim = true, dropEmpty = true, dedupe = true, ignoreCase = false, sort = "az" } = {}) {
let lines = text.split(/\r?\n/);
if (trim) lines = lines.map((l) => l.trim());
if (dropEmpty) lines = lines.filter(Boolean);
if (dedupe) {
const seen = new Set<string>();
lines = lines.filter((l) => { const k = ignoreCase ? l.toLowerCase() : l; return !seen.has(k) && !!seen.add(k); });
}
if (sort === "az") lines.sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }));
if (sort === "za") lines.sort((a, b) => b.localeCompare(a, undefined, { numeric: true, sensitivity: "base" }));
return lines.join("\n");
}
Make it production-ready
- Add shuffle with a Fisher–Yates loop.
- Count how many lines were removed.
Questions
Is the duplicate check case sensitive?
By default yes. Tick Ignore case to treat Apple and apple as the same line.
Free, runs in your browser, nothing is uploaded.