Line & Text Sanitizer

Text Utilities - Sort, Deduplicate, Reverse & Clean Text

Remove duplicate lines, sort text alphabetically, strip extra spaces, reverse string order, and encode/decode HTML entities.

6 lines Β· 45 characters

4 lines Β· 28 characters

How the sorter orders lines

Sorting uses the browser's language-aware comparison (localeCompare) rather than raw character codes. That means 'apple' and 'Apple' sit next to each other instead of every capitalised word coming before every lowercase one, as happens in a plain ASCII sort.

Numbers are compared as text, character by character. Sorting the lines item10, item2 and Item1 gives Item1, item10, item2, and the lines 9 and 10 sort as 10 then 9, because the character 1 comes before 9. If you need natural numeric order, pad numbers with leading zeros first (item02, item10) or sort in a spreadsheet.

A cleaning pipeline that works

Messy lists usually need several operations, and the order matters. Running them in this sequence gives a predictable result:

  • Remove extra spaces: collapses runs of spaces and tabs inside each line to a single space and trims both ends.
  • Remove empty lines: drops blank lines and lines that contain only whitespace.
  • Remove duplicate lines: keeps the first occurrence of each line and drops later repeats.
  • Sort A to Z, if the order of the original list does not matter.

Worked example

Start with eight lines: ' Mumbai', 'Delhi', a blank line, 'mumbai', 'Mumbai ', another blank line, 'Pune' and 'Delhi'. Running the four steps above in order gives four lines: Delhi, mumbai, Mumbai, Pune.

Two things are worth noticing. The padded ' Mumbai' and 'Mumbai ' were recognised as the same city once spaces were trimmed. 'mumbai' survived as a separate line because duplicate detection is case-sensitive. To merge case variants, convert the list to lowercase with the Case Converter first, then deduplicate.

If you deduplicate before removing blank lines, the first blank line is kept, since an empty line is treated like any other line and only its repeats are removed. That is another reason to remove empty lines early.

HTML escaping: what it protects and what it does not

Escaping replaces the five characters that have special meaning in HTML with entities, so text such as <b> is displayed literally instead of being read as a tag. It is the right tool when you want to show code samples on a web page or paste user-supplied text into HTML content.

Escaping is not the same as sanitising. It makes text safe to place between tags or inside a quoted attribute value, but it does not make a URL safe to use in an href, and it does nothing for text placed inside a script block. Also avoid escaping twice: &lt; escaped again becomes &amp;lt;, which displays as '&lt;' on the page. The unescape operation converts &amp; last, so a double-escaped string comes back one level at a time.

Reverse characters handles emoji correctly as single units, but letters built from a base character plus a separate accent mark may have the accent moved onto the neighbouring letter, so check reversed text that contains accented characters.

Working with data copied from spreadsheets and logs

Copying a single column from Excel or Google Sheets gives one cell per line, which is exactly the format these operations expect. Copying several columns gives tab-separated lines, and duplicate removal then compares whole rows, so two rows count as duplicates only if every cell matches.

Log files rarely contain exact duplicates because each line starts with a timestamp. Remove the timestamp portion first, for example with a spreadsheet split or a find-and-replace in your editor, and then deduplicate to see which distinct messages occurred.

Text Cleaners & Deduplicator: frequently asked questions

Why does 10 sort before 9?

Lines are compared as text, one character at a time, and the character 1 comes before 9. Add leading zeros (09, 10) so every number has the same length, or use a spreadsheet's numeric sort for number columns.

Is the sort case-sensitive?

Not in the way a plain code-based sort is. Letters are grouped alphabetically regardless of case, and when two lines differ only in case the lowercase version is placed first, so 'apple' comes before 'Apple'.

How do I count the unique items in a list?

Remove empty lines, then remove duplicates, and read the line count shown under the output. For case-insensitive counting, convert the list to lowercase first.

Does Remove extra spaces also remove tabs?

Yes. Any run of whitespace inside a line, including tabs, is replaced with a single space, and whitespace at the start and end of each line is removed. Line breaks are kept, so the list keeps one item per line.

All text processing suite