keepcalculator

06 Numbers and Powers

Sort by Substring: How It Works and When to Use It

Card No. 06·50692 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

Sorting by substring means arranging a list of strings based not on their full text, but on a portion of each string—for example, sorting filenames by their extension, or email addresses by their domain. You enter the strings and specify the substring (or the rule to extract it), and the calculator returns the list reordered by that substring. This is a common need in data cleanup, file management, and programming, and the approach depends on whether you are sorting by a fixed position, a delimiter, or a pattern.

Formula

The calculator extracts a substring from each input string using the given start and end positions (1-based, inclusive), then sorts the original strings by that extracted substring in ascending lexicographic order. The formula in words: for each string, take the characters from position s to position e, and use that as the sort key. If the string is shorter than e, the substring extends to the end of the string. The output is the full original strings, reordered.

Symbolically, for a list of strings L, the sorted result is:

sorted(L) = order L by key(x) = x[s..e]

where s is the start index (1-based) and e is the end index (inclusive). The comparison is case-sensitive and uses standard lexicographic order (e.g., ‘A’ < 'B' < ... < 'Z' < 'a').

Worked Example

Using the default input: strings are “apple”, “banana”, “cherry”. Start = 2, end = 3.

  • For “apple”, substring from position 2 to 3 is “pp”.
  • For “banana”, substring is “an”.
  • For “cherry”, substring is “he”.

Sorting the substrings lexicographically: “an” (banana) < "he" (cherry) < "pp" (apple). So the sorted order is: banana, cherry, apple. The calculator outputs that list.

When to Use Substring Sorting

Substring sorting is useful when the meaningful part of a string is not at the beginning. Common real-world cases:

  • Sorting filenames by extension (e.g., .txt, .jpg) when the extension is at the end.
  • Sorting email addresses by domain (after the @).
  • Sorting product codes by a specific segment (e.g., the 3rd to 5th characters).
  • Sorting log entries by timestamp that appears after a fixed prefix.

In all these, sorting by the full string would give a different, often less useful order.

How to Choose the Substring

To sort by a substring, you need to identify the exact start and end positions. This is straightforward when the substring is at a fixed position (like characters 4-6). But often the substring is delimited by a character (like a dot or @). In that case, you need to find the delimiter first. Many programming languages have functions like find() or indexOf() to locate a character, and then you can extract the substring. For example, to sort by domain, you would find the position of ‘@’ and take everything after it. This calculator assumes you already know the positions; if you don’t, you may need to preprocess your data.

Common Mistakes and Pitfalls

One common mistake is using 0-based indexing when you think in 1-based. This calculator uses 1-based (the first character is position 1). Another issue is case sensitivity: ‘a’ and ‘A’ sort differently. If you want case-insensitive sorting, you need to convert the substring to lowercase first. Also, if your strings vary in length, a substring that extends beyond the end will simply be shorter; this can affect the sort order unexpectedly. Finally, be careful with leading or trailing spaces in the substring—they are considered characters.

Comparison with Other Sorting Methods

Substring sorting is different from sorting by the full string, which is the default. It is also different from sorting by a numeric value extracted from the string (e.g., sorting by the number in a filename). For numeric substrings, you would need to convert the substring to a number first; otherwise, lexicographic order puts “10” before “2”. This calculator sorts lexicographically, so it is not suitable for numeric substrings without modification.

Limitations and Alternatives

This calculator handles only fixed-position substrings. If your substring is defined by a pattern (like after the last dot), you cannot specify that directly here. In such cases, you should use a spreadsheet formula (e.g., in Excel or Google Sheets) with functions like MID and FIND, or a script in Python or JavaScript. For example, in Python you could do: sorted(list, key=lambda x: x[start-1:end]). The calculator is a quick tool for simple cases, but for complex patterns, a programming approach is more flexible.

Frequently Asked Questions

Q: How do I sort by the last 3 characters of a string?
Set the start position to (length of string – 2) and end to the length. But since lengths vary, you need to know the length of each string. If all strings are the same length, it’s easy. If not, you may need to use a different method.

Q: Is the sorting case-sensitive?
Yes, this calculator sorts using standard lexicographic order where uppercase letters come before lowercase. If you need case-insensitive sorting, convert all substrings to lowercase before sorting.

Q: What happens if the substring start is greater than the string length?
The substring will be empty, and that string will sort before any string with a non-empty substring (since an empty string is the smallest). This may not be what you want; check your data.

Q: Can I sort by a substring that is not contiguous?
No, this calculator only extracts a contiguous range. For non-contiguous parts, you would need to concatenate multiple substrings, which is not supported here.

Q: How do I sort by the part after a specific character like @?
You need to find the position of that character first. Then set the start to that position + 1 and end to the string length. You can find the position manually or use a tool like a spreadsheet.

Q: Does this calculator handle numbers within the substring?
It sorts lexicographically, so numbers are treated as characters. For example, “10” comes before “2” because ‘1’ < '2'. If you want numeric sorting, you need to convert the substring to a number, which this calculator does not do.

Related

Formula used
in t | Strings (one per line) | - | apple,banana,cherry | 0
in s | Substring start (1-based) | - | 2 | 1
in e | Substring end (inclusive) | - | 3 | 1
out sorted | Sorted by substring | - | sortBySubstring(t, s, e) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →