CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2005
    Posts
    74

    how to sort data in txt file

    Hi All,

    I want to sort data in a txt file like Notepad. But my data is like table format. i mean there are rows and columns data. I want to sort one column, A, in descending. after sorting it, all rows are sorted based on column A sorted order and saving these data to txt file. could we sort the data like this? if so, pls share me the way.

    example,

    Before Sorting

    X Y Date Time Altitude Speed
    N 90.1243 E 29.4532 09/30/2007 10:24:12AM 00:00:30 34 ft 0.8 mph
    N 90.3456 E 29.3456 09/30/2007 10:24:12PM 00:10:30 10 ft 0.9 mph
    N 90.2143 E 29.1254 09/30/2007 09:24:12AM 00:05:30 20 ft 0.8 mph
    N 90.1043 E 29.3421 09/30/2007 06:24:12PM 00:25:30 34 ft 0.8 mph
    N 90.1673 E 29.4562 09/30/2007 10:00:12AM 01:15:30 34 ft 0.6 mph


    After Sorting(Main Sorting Column - Time)

    X Y Date Time Altitude Speed
    N 90.1673 E 29.4562 09/30/2007 10:00:12AM 01:15:30 34 ft 0.6 mph
    N 90.1043 E 29.3421 09/30/2007 06:24:12PM 00:25:30 34 ft 0.8 mph
    N 90.3456 E 29.3456 09/30/2007 10:24:12PM 00:10:30 10 ft 0.9 mph
    N 90.2143 E 29.1254 09/30/2007 09:24:12AM 00:05:30 20 ft 0.8 mph
    N 90.1243 E 29.4532 09/30/2007 10:24:12AM 00:00:30 34 ft 0.8 mph
    Last edited by soclose; May 5th, 2009 at 12:34 AM.

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: how to sort data in txt file

    Create a structure to hold each line of data, then create a list of the structures and sort the list. Then write each of the structures contents sequentially to a file.

    Optionally, you could do basically the same as above but implement the IComparable interface on the structures you created to hold the data and use the List<T>.Sort() method to sort the data based on the code you placed in the CompareTo() method implementation in each structure object.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Jan 2005
    Posts
    74

    Re: how to sort data in txt file

    could u give me an example? thanks

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: how to sort data in txt file

    RaleTheBlade gave you all the info you need. Do some MSDN or web search for IComparable, Sort(), CompareTo() and you should be able to find the right sample by yourself.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: how to sort data in txt file

    another soulution: you can create a datatable, fill it with your data and then sort it like described here http://www.csharpfriends.com/Forums/...x?PostID=36859
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Jan 2005
    Posts
    74

    Re: how to sort data in txt file and make column formated txt file

    Hi RaleTheBlade and Cilu,

    The data included and being formated in txt file is like my example above.

    I got to sort data in txt file using struct, List<T>.Sort() and CompareTo(), but this txt file is imported to MapInfo software. Here, this software reads each whole row in this file as a column. (I added spaces to seperate them, and headers using streamWriter.WriteLine.) However, I want to make columns in txt file. How should I? should i use csv file or other file type? pls direct me a way.

    When opening a txt file to Microsoft Excel, Excel ask us to choose file type : delimited or fixed width. The data columns in this txt file using fixed width can be read correctly by MapInfo software. I want to get a txt file like this.

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how to sort data in txt file and make column formated txt file

    Quote Originally Posted by soclose View Post
    The data columns in this txt file using fixed width can be read correctly by MapInfo software. I want to get a txt file like this.
    So if you have fixed width you can esily change to whatever you want to get your data read into a struct or class whatever and then sorting it. You also can have a method in your struct that recreates the original, but osrted pattern and save it for example to a csv for excel or in the same format using fixed size as it was in the beginning
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Jan 2005
    Posts
    74

    Re: how to sort data in txt file

    Hi All,

    Now I got the right file type for MapInfo software. I made all sorted data to csv file. After getting it, it's so simple and easy. Thanks All.

    Here, I found another file types : Delimited ASCII(*.txt) and MapInfo(*.tab). I searched their Info in MSDN but not. Please share me more info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured