如何在stringbuilder中对降序进行排序 [英] How to sort descending in stringbuilder

查看:1444
本文介绍了如何在stringbuilder中对降序进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Dim  sb =  StringBuilder()

对于 每个 msg
sb.AppendLine(msg.ToString())
下一步
Dim items As 列表( of 字符串)(sb.ToString()。分割( String (){vbCr& vbLf},StringSplitOptions.RemoveEmptyEntries))
items.Sort()' 按升序排序
sb = StringBuilder( String .Join(vbCr& vbLf,items.ToArray() ))





此items.Sort()按升序排序

我需要按降序排序



请指导我





即将获得



项目价值为







(0)=2015-04-22 JV RM在此保存您的清单
(1)=2014-12-28 SV在邮件中看到Word图像
(2) =2014-12-21 SV见word document0
(3)=2014-12-15 SV见字
(4)=2014-11-09 SV首先要开始的小项目这里编程。请参阅附件中的pdf
(5)=2015-01-04 SV我已将我的上一个版本放在您的计算机上。如果可能的话从那里开始







我还需要输出



 2015-04-22 JV RM将您的清单保存在这里
2015-01-04 SV我已将我的上一个版本放在您的计算机上。如果可能,请启动从那里
2014-12-28 SV查看邮件中的Word图片
2014-12-21 SV参见word document0
2014-12-15 SV见
2014- 11-09 SV这里开始编程的第一个小项目。请参阅附件中的pdf

解决方案

简单:

 items.Sort ()
items.Reverse()



或者,使用Linq方法OrderByDescending [ ^ ]而不是Sort


假设 lines 是ToString对它们进行格式化的对象的集合。 (即,他们不是已经是字符串。如果 msg 字符串,则省略.ToString( ).AddRange()。)

.AddRange .Split 的唯一原因c $ c>如果任何 msg 对象可以生成空字符串或生成多个行文本。如果不是这样,那么 .Split()是不必要的。

 私有 共享 功能 ReverseComparison(_ 
ByVal x 作为 字符串 ByVal y 作为 字符串作为 整数
返回 y.CompareTo(x)
结束 功能

Dim items As 列表( of 字符串)()
Const CrLf As String = vbCr& vbLf
对于 每个 msg
items.AddRange(msg.ToString()。Split(CrLf,StringSplitOptions.RemoveEmptyEntries))
下一步
项目。排序( AddressOf ReverseComparison)
Dim sb = StringBuilder(字符串 .Join(CrLf,items.ToArray()))



未经测试,我的VB有些生疏。 ; - )


Dim sb = New StringBuilder()

        For Each msg In lines
            sb.AppendLine(msg.ToString())
        Next
        Dim items As New List(Of String)(sb.ToString().Split(New String() {vbCr & vbLf}, StringSplitOptions.RemoveEmptyEntries))
        items.Sort()'this sorts in ascending 
        sb = New StringBuilder(String.Join(vbCr & vbLf, items.ToArray()))



This items.Sort() Sorts in ascending order
I need to sort in descending

Please Guide me


im getting

values of item as



(0) = "2015-04-22 JV RM  Save your list here"
(1) = "2014-12-28 SV See Word  Image in the mail"
(2) = "2014-12-21 SV See word document0"
(3) = "2014-12-15 SV See word"
(4) = "2014-11-09 SV First small items to start programming here. See the pdf in attach"
(5) = "2015-01-04 SV I have placed my last version on your computer. If possible start from there"




also I need output as

2015-04-22 JV RM  Save your list here
2015-01-04 SV I have placed my last version on your computer. If possible start from there
2014-12-28 SV See Word  Image in the mail
2014-12-21 SV See word document0
2014-12-15 SV See word"
2014-11-09 SV First small items to start programming here. See the pdf in attach

解决方案

Simple:

items.Sort()
items.Reverse()


Alternatively, use the Linq method OrderByDescending[^] instead of Sort


Assuming that lines is a collection of objects whose ToString formats them. (I.e., they aren't already Strings. If the msg values are Strings, then omit the .ToString() in the .AddRange().)
The only reason for the .Split in the .AddRange is if any of the msg objects can generate an empty string or generate multiple "lines" of text. If that is not the case, then the .Split() is unnecessary.

Private Shared Function ReverseComparison( _
  ByVal x As String, ByVal y As String) As Integer
  Return y.CompareTo(x)
End Function

Dim items As New List(Of String)()
Const CrLf As String = vbCr & vbLf
For Each msg In lines
   items.AddRange(msg.ToString().Split(CrLf, StringSplitOptions.RemoveEmptyEntries))
Next
items.Sort(AddressOf ReverseComparison) 
Dim sb = New StringBuilder(String.Join(CrLf, items.ToArray()))


Untested, my VB is somewhat rusty. ;-)


这篇关于如何在stringbuilder中对降序进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆