vb.net函数File.txt以a-z 0-1顺序保存

时间:2017-07-18 01:17:36

标签: vb.net function file sorting

我有一个功能,  我希望按行保存行;

  • 甲/ Z
  • 0/9990 periodic

大家好,谢谢你。

我的功能就是这个;

Dim WorksList As String = "c:\db\works.txt"

Public Function WorksListSave()
   Dim wl As New System.Text.StringBuilder()
   For Each o As Object In ComboBox_WorkName.Items
       wl.AppendLine(o)
   Next

   File.WriteAllText(WorksList, wl.ToString())

   Return False

End Function

1 个答案:

答案 0 :(得分:0)

定义逻辑以比较像这样的函数中的字符串:

 Private Function MyComparer(x As String, y As String) As Integer
    ' TODO: Add tules compare x to y.
    ' if x > y then return 1
    ' if x = y then return 0
    ' if x < y then return -1
End Function

首先将您的项目转换为List(字符串)

Dim MyList As List(Of String) = (From obj In ComboBox_WorkName.Items Select CStr(obj)).ToList()

然后使用您的比较器Sort列表:

MyList.Sort(AddressOf MyComparer)

将您的列表写入您的文件:

File.WriteAllLines(WorksList, MyList)