c# in VS console Sorting problem

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi all,

I am trying to do a simple program to keep some information on components. The user is asked to input the components name, description, new or used, and price bought. A component number is given automatically by the program, where with each number the code CMP is added (ex: for components 1: CMP1).
Now my problem is to sort the components by their name. I did come research and found that merge or quick sort are the best methods to use. I did found some tutorials by they only sort individual characters or numbers.
How can I use these sorting methods when you have a text file (.txt) full of information? How will the program know which are the components numbers? and how will the program know which information must be shifted with the respective component number?

Any help would be highly appreciated.
 

ErnieM

Joined Apr 24, 2011
8,377
Sounds like you have a single text file full of information records where each record is a collection of some records. That is the start of a data base. To search and sort you must first parse out the data from each record then target the field you wish to sort. This is no simple task when starting from basically zero.

A complete task will be accomplished if you start from something with most of the functionality built in. While Microsoft Access will do exactly this it has a high learning curve. Something simpler such as Excel probably has enough strength to do this (and several other tasks you’ll want) by itself. You can make a spreadsheet programmable if you open up the Visual Basic window.

Unless your C# has a database ability I would ditch it over something where the heavy lifting is already coded in.
 

vpoko

Joined Jan 5, 2012
267
.NET has good functionality for sorting. First, you have to create an in-memory structure (a class) to represent a single record of your database (e.g., component#, description, whatever other fields you have). The class needs to implement the IComparable interface, which will require you write a method which takes another instance of your class as an argument and compares it to the instance on which the method is called. You then create some kind of collection of these class instances to represent your entire DB (you can use an array, an ArrayList, or others), and then call sort() on the collection, which will use the comparator you defined to sort the list automatically, using an unstable quicksort.
 
Last edited:
Top