c# in VS console Sorting problem

Discussion in 'Programmer's Corner' started by Dritech, Dec 19, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    What platform and language are you using?
    Most high level languages allow string compares.
     
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi,
    Thanks for the reply.
    I and using C#. But does the string comparison works using the linear search technique?
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Why not test it and find out.
    Look at strcmp and strcoll.
     
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks. I will type these methods.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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.
     
  7. vpoko

    Member

    Jan 5, 2012
    258
    47
    .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: Dec 22, 2014
  8. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    Just to add to what vpoko said you want "List" in C#, "ArrayList" is a Java class.

    Google "C# Sort"
     
    vpoko likes this.
  9. vpoko

    Member

    Jan 5, 2012
    258
    47
    Oops, thanks, you're 100% right, it's List<T> in C#.
     
Loading...