Data - How to sort by partial numbers in a text field

The ability to sort a list of records by a number that appears in a text field can be challenging.

As an example, let's consider the following list of chapter names from an Excel table called ChapterDetails. If we attempt to sort this data by the 'chapter name' column, the output will be sorted by text sequence, and not by the sequence of the number that appears in the chapter name.

The target sort sequence of this data would be "Ch1, Ch2, Ch3....", instead of "Ch1, Ch10, Ch11...".


How to extract the numbers from a text field

To sort this data by the number that appears within the text, it's necessary to extract the number and to sort by the extracted value.

To do this, we can call the Match function to return the digits in the ChapterName field. We can wrap this in a call to AddColumns, to add the output to a column called ChapterNum. Here's how the formula looks.
 
AddColumns(ChapterDetails,
"ChapterNum",
Value(Match(ChapterName, MultipleDigits).FullMatch)
)

If we were to collect this output to a collection, we would see this result.


Note that there is a caveat to this technique in that the Match expression will extract all numbers in the field. Therefore, it would be necessary to incorporate additional text manipulation syntax, if there are numbers present in other parts of the text.

As an example, here is the syntax to extract only the numbers that appear within the first five characters of the chapter name text.
 
AddColumns(ChapterDetails,
"ChapterNum",
Value(Match(Left(ChapterName,5), MultipleDigits).FullMatch)
)

Sorting by the extracted number value

Once we build the formula to extract the number into a new column, we can call the Sort/SortByColumn functions to sort the data by the numeric value.

As a demonstration, we can sort the items in a gallery or data table control by setting the Items property to the following formula.

SortByColumns(
AddColumns(ChapterDetails,
"ChapterNum",
Value(Match(ChapterName, MultipleDigits).FullMatch)
),
"ChapterNum", Ascending
)

The output now appears in the expected sequence, as shown in the screenshot beneath.

Conclusion

To sort a table/list of records by a number that partially appears in a field, we can extract and add the number to a new column by calling the Match and AddColumn functions. We can then sort the records by the numeric value by calling the Sort or SortByColumns function.

Related posts

Data - How to remove trailing comma all rows in a table
February 20, 2025
Data - How to find the common rows from 3 or more collections
October 06, 2024
Data - How to show the distinct rows from 2 data sources or collections
February 26, 2024
Data - How to implement circular rotational date sorting
February 21, 2024
Bug - What to do when the data section of the Power Apps Maker portal doesn't work
June 18, 2023
Data - Combine columns from separate tables into a single table
October 13, 2022
Formula - Transposing/converting rows to columns- an almost impossible task?
September 23, 2021
Data - How to rename field names in a record
July 14, 2021
Data - How to hide duplicate rows in a gallery / show distinct multiple columns in a gallery
July 09, 2021
Data - Retrieving news/forum/blog articles with RSS
June 26, 2021
Data - How to return the last record from a table
June 19, 2021
Data - How to create bulk test/dummy records with random values
June 18, 2021
Data - 3 things you should know before using the MySQL or PostgreSQL connectors
May 11, 2021
Data - A walkthrough of how to migrate the data source of an app from Excel to Sharepoint
April 26, 2021
Data - How to enforce unique values (or prevent duplicate values) in one or more columns
April 19, 2021
Data - How much mobile data does Power Apps consume? What ways can we minimise this?
March 28, 2021
Data - How to save and retrieve Google calendar entries
March 14, 2021
Data - How to save and retrieve Google contacts
March 10, 2021
SQL - Caution! This is how users can hack shared SQL connections
January 23, 2021
SharePoint – 2 Mistakes to avoid when importing Excel data
January 10, 2021
SQL - Don't let this DateTime bug catch you out!
January 05, 2021
Settings - What's the purpose of the "Explicit Column Selection" Setting?
January 04, 2021
SQL Server for Beginners Part 3 - Installing On-Premises Gateway
January 24, 2019
SQL Server for Beginners Part 2 - Installing Management Studio
January 14, 2019
SQL Server for Beginners Part 1 - Installing SQL Server
January 04, 2019
Searching data–What you need to know about case sensitivity
December 27, 2018
Images - How to create images that can change depending on data
November 09, 2018
Excel - Reasons NOT to use Excel as a data source
September 25, 2018
SharePoint - What you need to know about Filtering Data
September 16, 2018
Formulas - Generating Row Numbers
April 05, 2018