How to count the number of lines in a file using Bash?

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

Was this solution helpful to you?

Find solution

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.