如何在字符串列表中对字符串日期进行排序?
输入:
2019/5 / 9、2019 / 5 / 13、2019 / 5 / 14、2019 / 5 / 17、2019 / 5 / 19、2019 / 5 / 15、2019 / 5/18
预期输出:
2019/5 / 9、2019 / 5 / 13、2019 / 5 / 14、2019 / 5 / 15、2019 / 5 / 16、2019 / 5 / 17、2019 / 5/18
我的代码:
Dim Mytargetlist As String = ""
Dim list As New List(Of String)
Mytargetlist = p(1) + "/" + p(2) + "/" + p(3)
list.Add(Mytargetlist)
'list.Sort()
Dim result As List(Of String) = list.Distinct().ToList
If Session("mydata") Is Nothing Then
Session("mydata") = New ArrayList
End If
TryCast(Session("mydata"), ArrayList).Add(result.ToArray())
答案 0 :(得分:1)
如果要对日期列表进行排序,为什么不使用日期列表?
Dim list As new List(of Date)
list.add(New Date(2019,5,9))
list.add(New Date(2019,5,9))
list.add(New Date(2019,5,01))
list.add(New Date(2019,5,25))
list.add(New Date(2019,5,13))
list.add(New Date(2019,5,30))
list.add(New Date(2019,5,9))
list.Sort()
Dim myFormat = "dd/MM/yyyy"
For Each item As Date In list
Console.WriteLine(item.ToString(myFormat))
Next
输出:
01/05/2019
09/05/2019
09/05/2019
09/05/2019
13/05/2019
25/05/2019
30/05/2019
答案 1 :(得分:1)
要回答所提出的问题,您可以调用 sendVirtualStickDataTask = new SendVirtualStickDataTask(0, -5, 0, 0);
sendVirtualStickDataTimer = new Timer();
sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
private class SendVirtualStickDataTask extends TimerTask {
private float pitch;
private float roll;
private float yaw;
private float throttle;
private long startTime = System.currentTimeMillis();
public SendVirtualStickDataTask(float inputPitch, float inputRoll, float inputYaw, float inputThrottle) {
pitch = inputPitch;
roll = inputRoll;
yaw = inputYaw;
throttle = inputThrottle;
}
@Override
public void run() {
if (System.currentTimeMillis() - startTime > 300) {
mFlightController.sendVirtualStickFlightControlData(new FlightControlData(0, 0, 0, 0),
new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(final DJIError djiError) {
}
});
cancel();
} else {
mFlightController
.sendVirtualStickFlightControlData(new FlightControlData(pitch,
roll,
yaw,
throttle),
new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(final DJIError djiError) {
}
});
}
}
}
的重载,该重载需要一个Sort
的委托,并提供一个将Comparison(Of T)
与Strings
比较的Lambda:>
Dates
为了比较,将每个值转换为实际的list.Sort(Function(s1, s2)
Dim d1 As Date
Dim d2 As Date
If Date.TryParse(s1, d1) AndAlso Date.TryParse(s2, d2) Then
'Both values convert so compare chronologically.
Return d1.CompareTo(d2)
End If
'At least one value does not convert so consider them equivalent.
Return 0
End Function)
。如果您只想成功转换一个值时想做一些特定的事情,例如无效值总是排在最后,您也可以实现。
也就是说,除非您有充分的理由将日期存储在Date
中,否则您真的应该首先使用Strings
值。