TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# Sort Number Strings

Sort strings that contain numbers at their starts by using logic to parse the numbers.
Sort number strings. Data files sometimes contain a leading integer on each line. When you want to sort these lines, you cannot use a textual sort on the strings. Instead, you must take into account the leading number by parsing it.Sort
Input file: 2 - pa 2 - zo 23 - zo 3 - ad 3 - za Required output: 2 - pa 2 - zo 3 - ad 3 - za 23 - zo
Example. The idea behind this code is that each line, as it is read from the resource file, is also parsed and stored into an object. The leading integer on each line is stored as an int field in the object.

And: The Line object implements IComparable: in CompareTo, it first compares the integer fields and then the characters following.

IComparableToStringStreamReader
C# program that sorts strings with numbers using System; using System.Collections.Generic; using System.IO; class Line : IComparable<Line> { int _number; string _afterNumber; string _line; public Line(string line) { // Get leading integer. int firstSpace = line.IndexOf(' '); string integer = line.Substring(0, firstSpace); this._number = int.Parse(integer); // Store string. this._afterNumber = line.Substring(firstSpace); this._line = line; } public int CompareTo(Line other) { // First compare number. int result1 = _number.CompareTo(other._number); if (result1 != 0) { return result1; } // Second compare part after number. return _afterNumber.CompareTo(other._afterNumber); } public override string ToString() { return this._line; } } class Program { static void Main() { List<Line> list = new List<Line>(); using (StreamReader reader = new StreamReader("c:\\p.txt")) { while (true) { string line = reader.ReadLine(); if (line == null) { break; } list.Add(new Line(line)); } } list.Sort(); foreach (Line value in list) { Console.WriteLine(value); } } } Output 2 - pa 2 - zo 3 - ad 3 - za 23 - zo
Alphanumeric sort. A more general-purpose alphanumeric sorting algorithm would also work in this case. The alphanumeric sort would eliminate the need to parse lines on your own. But it would also reduce the quality of your object model.Alphanumeric Sort

Note: The solution presented above makes many things possible by actually storing the integer as a field.

For example, the Line objects could be used to sum all the integers on the lines or compute an average number. Further processing could be done on the lines as they are added to validate them and report possible errors in the data file.
Summary. In programming, choosing the right object model for your problem can make a big difference. In this example, using an object that stores the integer as a field allows a custom sorting method to be used.

Thus: In the C# language, implementing the IComparable interface and using a custom constructor is one approach that succeeds.

© 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