Results 1 to 9 of 9

Thread: Creating a sorted file.

  1. #1

    Thread Starter
    New Member Yankeepop's Avatar
    Join Date
    Jan 2010
    Location
    Henderson, NC - USA
    Posts
    5

    Creating a sorted file.

    I wrote a membership program for our local senior center but I can't figure out how to create a alphabetized file so I can send it to a printer.

    For instance:

    File Names.dat contains
    Mary
    Jack
    Sally
    Andrew

    I need to create a new alphabetized file.

    I know a simple bubble sort routine will do this but I don't know how to do it. Can someone please help me?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Creating a sorted file.

    Try this:
    vb.net Code:
    1. Dim myNames() As String = File.ReadAllLines("C:\File Names.dat")
    2. Dim sortedNames = (From name As String In myNames Order By name).ToArray
    3. File.WriteAllLines("C:\File Names.dat", sortedNames)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Creating a sorted file.

    Umm... C# code:

    C# Code:
    1. string[] myNames = File.ReadAllLines("C:\\File Names.dat");
    2. string[] sortedNames = (from name in myNames orderby name select name).ToArray();
    3. File.WriteAllLines("C:\\File Names.dat", sortedNames);
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    New Member Yankeepop's Avatar
    Join Date
    Jan 2010
    Location
    Henderson, NC - USA
    Posts
    5

    Re: Creating a sorted file.

    Thanks Predeep. I'm using VB5.0 I'm just looking for a simple bubble sort routine to open an existing file and create a new sorted file.

  5. #5
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Re: Creating a sorted file.

    Here in the C# section might not be the best place to post a vb5 question, it could take a long time for someone to give you a usable responce.
    C:\DOS
    C:\DOS\RUN
    RUN\DOS\RUN

  6. #6

    Thread Starter
    New Member Yankeepop's Avatar
    Join Date
    Jan 2010
    Location
    Henderson, NC - USA
    Posts
    5

    Re: Creating a sorted file.

    Thank you Scottlafoy

    This is the first time I used the forum and didn't realize I was in the C# section.

    I'll repost in the right section if I can figure out this forum.

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Creating a sorted file.

    Try something like this:

    (not tested code)
    vb Code:
    1. Private Sub SortArray(ByRef TheArray() As String)
    2.     Dim temp As String, i As Integer, j As Integer
    3.     For i = 0 To UBound(TheArray)
    4.         For j = i To UBound(TheArray)
    5.             If TheArray(i) > TheArray(j) Then
    6.                 temp = TheArray(i)
    7.                 TheArray(i) = TheArray(j)
    8.                 TheArray(j) = temp
    9.             End If
    10.         Next
    11.     Next
    12. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Creating a sorted file.

    Thread Moved
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Creating a sorted file.

    Based on Rhino's idea in this post: http://www.vbforums.com/showpost.php...34&postcount=2
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim strTemp As String
    Dim FF As Integer
    FF = FreeFile '~~> Get free file handle
    Open "c:\abc.txt" For Input As #FF  '~~~> open the file for reading data
    While Not EOF(FF)
      Line Input #FF, strTemp
      List1.AddItem strTemp '~~~> Adding the names to the list
    Wend
    Close #FF
    Open "c:\abc.txt" For Output As #FF '~~~>Saving back, the sorted names
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
      Print #FF, List1.List(i)
    Next
    Close #FF
    MsgBox "finished"
    End Sub
    List1's Sorted property was set to True at design time

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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