Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 1-Oct-15 12:22pm
v3
Comments
Matt T Heffron 1-Oct-15 17:36pm    
Why are you first appending all of the lines into the StringBuilder before splitting? Aren't they individual lines to start with?

Simple:
VB
items.Sort()
items.Reverse()

Alternatively, use the Linq method OrderByDescending[^] instead of Sort
 
Share this answer
 
Comments
Bensingh 30-Sep-15 6:49am    
Thanks a lot for this nearly 2 hours I have spend
OriginalGriff 30-Sep-15 7:07am    
:laugh:
Happens to us all!
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.
VB
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. ;-)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900