简体   繁体   English

如何对不同时间的数组进行排序?

[英]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.所以我有一个包含各种时间段的数组,但我想从最早到最新的时间进行排序,但是,我还想删除数组中时间值为 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.如果您使用字符串数组只是使用DateTime列表和Sort方法,那么使用字符串数组并不是一个好主意。

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: .Net 中没有用于从数组中删除特定项目的内容,但可以使用 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:一个更有效的替代方法是使用像ListSortedSet这样的泛型集合:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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