Sorting a text file visual basic?
-
Imports System.IO Public Class Form1 Public Structure ExamGrades Dim name As String Dim grade As Integer End Structure Dim Values() As String Dim Count As String = 0 Dim grades As New List(Of ExamGrades) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim lines() As String = IO.File.ReadAllLines("SCORES.TXT") For x As Integer = 0 To lines.GetUpperBound(0) Step 2 grades.Add(New ExamGrades With {.name = lines(x), .Grade = CInt(lines(x + 1))}) Next End Sub Private Sub BtnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDisplay.Click ListBox1.Items.Clear() ListBox1.Items.Add("Original Data") ListBox1.Items.Add("===========") ListBox1.Items.AddRange(Array.ConvertAll… Function(eg) eg.name & " " & eg.grade.ToString)) ListBox1.Items.Add("") ListBox1.Items.Add("Sorted Data") ListBox1.Items.Add("===========") Dim lines() As String = IO.File.ReadAllLines("SCORES.TXT") For x As Integer = 0 To lines.GetUpperBound(0) Step 2 grades.Add(New ExamGrades With {.name = lines(x), .Grade = CInt(lines(x + 1))}) Next Dim index As Integer = 0 Dim lineoftext As String = "" Using sr As New System.IO.StreamReader("SCORES.txt") While Not sr.EndOfStream lineoftext = sr.ReadLine If lineoftext <> "" Then ReDim Preserve Values(index) Values(index) = lineoftext index += 1 End If End While End Using Array.Sort(Values) For Each s As String In Values ListBox1.Items.Add(s) Next End Sub I am attempting to sort a text file Scores.txt below is the text file Anderson 98 Jones 87 Smith 100 Wilson 75 Young 91 I need to have the numbers sorted...but have the names stay with the numbers so it outputs Smith 100 Anderson 98...etc I have been trying to do a bubble array and such but I keep screwing it up...right now it sorts the first number 100 then it just randomly has the rest of the numbers...followed by the names alphabetized after the numbers How would i go about have the rest of the numbers sorted correctly...and also have the names attached as i mentioned? thanks for the help!
-
Answer:
If you go an easy way, you can create a one-dimensional array (each element is string like this: “score” & “key symbol” & “name”) and use Array.Sort and Array.Reverse methods. When you display elements of sorted array in list box, just switch places of “name” and “score”, and replace “key symbol” with space: Dim ExamGrades() As String Dim Lines() As String = IO.File.ReadAllLines("C:\...\Text.txt") For x As Integer = 0 To Lines.GetUpperBound(0) Step 2 ReDim Preserve ExamGrades(x / 2) ExamGrades(x / 2) = Lines(x + 1) & "~" & Lines(x) Next Array.Sort(ExamGrades) Array.Reverse(ExamGrades) For Each Element As String In ExamGrades ListBox1.Items.Add _ (Strings.Right(Element, Len(Element) - InStr(Element, "~")) _ & " " & Strings.Left(Element, InStr(Element, "~") - 1) & vbCrLf) Next
Petro at Yahoo! Answers Visit the source
Related Q & A:
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
- Codes for a simple visual basic program.Best solution by vbtutor.net
- Is java or visual basic a machine level, low level, high level or binary level programming language?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.