简体   繁体   中英

How do I sort an array of different times?

So I have an array of various timeslots, but I want to sort out the times from the earliest ones to the latest ones, however, I also want to delete any elements within the array that has the time value of 12:00am.

Appointment(0) = #10:45:00 AM#
Appointment(1) = #12:00:00 AM# 'My actual has up 80 elements of different timeslots but I'm using 3 elements as an example
Appointment(2) = #12:00:00 AM#

Is there anyone who can help me with this problem?

Using a string array is not a good idea if that's what you used just use a list of DateTime and the Sort method.

Dim list As List(of DateTime) = new List(of DateTime)
For Each val As String In Appointment
    list.Add(val.replace(" ",""))
Next
list.Sort(New Comparison(Of Date)(Function(x As Date, y As Date) y.CompareTo(x)))

Nothing in .Net for removing specific items from array, but it can be "simplified" a bit with LINQ:

Dim Appointment = {#10:45#, #12AM#, #12AM#}

Appointment = (From a In Appointment Where a <> #12AM# Order By a).ToArray

or

Appointment = Appointment.Except({#12AM#}).ToArray
Array.Sort(Appointment)

A bit more efficient alternative can be to use generic collection like List or SortedSet instead:

Dim list = New List(Of Date) From {#10:45#, #12AM#} ' or Dim list = Appointment.ToList
list.Add(#12AM#)

list.RemoveAll(Function(a) a = #12AM#)
list.Sort()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM