简体   繁体   English

Array.Sort()通过字符串tkey

[英]Array.Sort() by string tkey

I have two arrays; 我有两个数组; indexArray() as String , and valueArray() as Decimal . indexArray() as StringvalueArray() as Decimal

indexArray is actually a list of numbers, in string format. indexArray实际上是数字列表,为字符串格式。 I'd like to sort the indexArray (while keeping corresponding values in valueArray the same) by ascending numerical order of its decimal counterparts. 我想通过升序十进制对应的数字顺序来排序indexArray(同时保持valueArray中的对应值相同)。

Here is what I've tried: 这是我尝试过的:

Array.Sort(Of String, Decimal)(indexArray, valueArray, Nothing)

I've looked into IComparer objects, but I couldn't get a good grasp on how to make one do what I need, but I'm certain that's likely the answer; 我已经研究了IComparer对象,但是我无法很好地掌握如何使自己做我需要的事情,但是我确信这可能是答案。 or maybe something even simpler that I don't know enough to think of! 甚至更简单的东西,我都不知道!

Not sure if there is another way, so let's wait if someone has a better idea, but you could use a list of a custom class to store your values and then ask Linq OrderBy method to sort your list of values 不确定是否还有其他方法,因此,如果有人有更好的主意,让我们等待,但是您可以使用自定义类的列表来存储您的值,然后要求Linq OrderBy方法对值列表进行排序

Sub Main
    Dim indexArray() as String = {"1.1", "12.2", "10.3", "2.4", "2.1"}
    Dim valueArray() as Decimal = {1.1, 12.2, 10.3, 2.4, 2.1}

    Dim listOfValues = new List(Of MyValues)
    for x = 0 to indexArray.Length - 1
        listOfValues.Add(new MyValues() With {.stringValue = indexArray(x), .decimalValue = valueArray(x)})
    Next

    Dim result = listOfValues.OrderBy(Function(x) x.decimalValue)

    for each mv in result
        Console.WriteLine(mv.stringValue)
    Next

End Sub

Class MyValues
    Public stringValue as String
    Public decimalValue as Decimal
End Class

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

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