TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Sort Number Strings

Parse lines and then sort them based on a number. Sort strings with numbers in them.
Sort number strings. Lines in a file contain several parts. We want to sort them based on an integer within the line. We can do this by parsing the lines into objects and then sorting those objects with a CompareTo Function.
Example. This program has many parts. But first we see the data file used in the program. Please place it in an accessible location on your computer and then adjust the StreamReader to access that location.StreamReader

Line: The Line class inherits from IComparable. It has a New method, where we parse the String data. And it implements CompareTo.

IComparable

New: In the New subroutine we use IndexOf to search for the first space. We then parse the integer and store it. We also store the strings.

CompareTo: In CompareTo, we have to compare two Line instances. We first compare the number stored on each object.

And: This is the number we parsed in the New subroutine. If those are equal, we compare the remaining parts of the lines.

Data file: p.txt 2 - pa 2 - zo 23 - zo 3 - ad 3 - za VB.NET program that parses lines into objects Imports System.IO Class Line Implements IComparable(Of Line) Dim _number As Integer Dim _afterNumber As String Public _line As String Public Sub New(ByVal line As String) ' Here we parse the integer digits before the first space. Dim firstSpace As Integer = line.IndexOf(" "c) Dim digits As String = line.Substring(0, firstSpace) ' Store data in class fields. _number = Integer.Parse(digits) _afterNumber = line.Substring(firstSpace) _line = line End Sub Public Function CompareTo(other As Line) As Integer _ Implements IComparable(Of Line).CompareTo ' Compare first based on number at the start of the line. ' Then compare the string parts. Dim result1 As Integer = _number.CompareTo(other._number) If Not result1 = 0 Then Return result1 End If Return _afterNumber.CompareTo(other._afterNumber) End Function End Class Module Module1 Sub Main() Dim lines As List(Of Line) = New List(Of Line)() ' Read lines in from this file. Using reader As New StreamReader("C:\\programs\\p.txt") While True Dim line As String = reader.ReadLine If line = Nothing Then Exit While End If lines.Add(New Line(line)) End While End Using ' Sort lines based on IComparable. lines.Sort() ' Display original lines in sorted order. For Each value As Line In lines Console.WriteLine(value._line) Next End Sub End Module Output 2 - pa 2 - zo 3 - ad 3 - za 23 - zo
In the Main Sub, we create a StreamReader in a Using-statement. We read in the text file. And we parse it by passing each line to the Line constructor (the New Sub). Finally we sort the lines List—this invokes the CompareTo Function.

So: The lines are all sorted by their leading Integers, and then by their remaining characters.

Often, this approach is superior to more complex methods that sort strings. With this style of code, we gain an object model of our data. So it can be tested and changed in many ways. This makes programs more powerful.

And: The code here is less confusing than alternative approaches like the alphanumeric sorting methods I have developed.

Alphanumeric Sort
Summary. A preliminary step before sorting data is sometimes needed. With this sample code, we create a simple object model from text lines. This enables more powerful sorting of the data.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf