0

So i have created a program ,that takes a certain list of lines and reads the first 21 letters[Date - Time(01.02.2015 - 18:30:25)] from every line and split them. Now i want to check every line and and sort it depending on date (Newer to older).

What I have tried: I created a number from all date and time giving priority to Year>Month>Day>Hour>Minute>Second (Example: "01.02.2015 - 18:30:25" = 20150201183025) and then for every line that the program reads it reads also the previous(starting from the second line).

My sorting algorithm is:

Dim temp As String = ""
For i As Integer = 1 To Lines1.length
    If fNumber >= sNumber Then
        temp = Lines1(i - 1)
        Lines1(i - 1) = Lines1(i)
        Lines1(i) = temp
    End If
Next i

Lines1() is the array of strings that I want the program to check

fNumber is the Number from the Lines(i-1)

and sNumber is the Number from the Lines(i)

But my result is the exact same list with no changes.

Can anyone help?

4
  • 3
    Use DateTime.ParseExact to convert it to a date and then sort it Commented Jan 27, 2015 at 13:06
  • that is an array, not a List. there is not enough code to help. What is the declaration and where did fNumber come from. Probably ought to turn on Option Strict Commented Jan 27, 2015 at 13:07
  • Full code This is the full code Commented Jan 27, 2015 at 13:16
  • If your goal is not to implement a sorting function but just get the sorting done, you would be better served using LINQ or learning how to use the built in Sort function. Commented Jan 27, 2015 at 13:32

2 Answers 2

3

Why do you try to sort the list by your own algorithm?

Let the framework handle that for you. Just convert the date information into a sane datatype (DateTime) and order it using OrderByDescending.

Example:

Dim Lines1 = {"01.02.2015 - 18:30:25",
              "01.06.2011 - 18:30:25",
              "11.02.2012 - 11:34:25",
              "01.07.2010 - 18:30:25",
              "01.01.2010 - 12:30:25"}

Dim c = System.Globalization.CultureInfo.InvariantCulture
Dim ordered = From s in Lines1
              Let dt = DateTime.ParseExact(s, "dd.MM.yyyy - HH:mm:ss", c)
              Order By dt Descending
              Select dt

ordered is now

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

Here's an approach:

Function ParseSort(fromLines As String()) As List(Of String)
    ParseSort = New List(Of String)
    Dim sortingList = New List(Of KeyValuePair(Of Date, String))

    For Each line In fromLines
        Dim toParse = line.Substring(0, 21)
        Dim dateValue = Date.ParseExact(toParse, "MM.dd.yyyy - HH:mm:ss", Nothing)
        sortingList.Add(New KeyValuePair(Of Date, String)(dateValue, line))
    Next

    sortingList.Sort(New Comparison(Of KeyValuePair(Of Date, String))(Function(x As KeyValuePair(Of Date, String),
                                                                               y As KeyValuePair(Of Date, String)) Date.Compare(x.Key, y.Key)))

    For Each pair In sortingList
        ParseSort.Add(pair.Value)
    Next
End Function

This actually sorts using dates rather than treating your date string from each line as some number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.