简体   繁体   English

Array.sort()无法正常工作

[英]Array.sort() is not working correctly

Suppose an array consists of values: 假设数组由值组成:

dim arrnum() as integer=nothing

arrnum consists of values (3,8,6,15,2,10) arrnum由值(3,8,6,15,2,10)组成

while using Array.sort(arrnum) 

valuse are sorted like way. valuse的排序方式类似。 (10,15,2,3,6,5) (10,15,2,3,6,5)

Actually I need values in a sorted way. 实际上,我需要以一种排序的方式提供值。

Can you help me out? 你能帮我吗?

Assuming your sorted array is actually 10, 15, 2, 3, 6, 8 (ie, containing the same elements as the original), it looks like it's sorting lexicographically (a fancy way of saying string sorting rather than numeric sorting). 假设你的排序后的数组实际上是10, 15, 2, 3, 6, 8 (即,包含相同的元素作为原始),它看起来像它的排序按字典顺序(称字符串排序而不是数字排序的一个奇特的方式)。

In other words, 10 and 15 are less than 2 if you're sorting based on character data, since 1 is less than 2 . 换句话说,如果您基于字符数据进行排序,则1015小于2 ,因为1小于2

That makes me doubt the veracity of your statement that they're actually integers, so that would be the first thing I'd be checking. 这使我怀疑您的声明是否实际上是整数的准确性,因此这将是我要检查的第一件事。

I think you have some mistake to define array Take look below code this worked 我认为您在定义数组时犯了一些错误,请看下面的代码

Dim arrnum() As Integer = Nothing
arrnum = {3, 8, 6, 15, 2, 10}
Array.Sort(arrnum)

Unsuprisingly, the code 毫无疑问,代码

Sub Main
    Dim array = {3, 8, 6, 15, 2, 10}
    System.Array.Sort(array)
    Console.WriteLine(String.Join(", ", array))
    Console.ReadKey()
End Sub

works entirely as I would expect. 完全按照我的预期工作。

You are doing something(s) wrong. 您做错了什么。

Before we can help you out you need to show us exactly what you are doing. 在我们为您提供帮助之前,您需要确切地向我们展示您的工作。

I'm drawn to suggest you are sorting an array of String but the 6 and 5 in your results are reversed for alphabetical order. 我建议您对String数组进行排序,但结果中的65会按字母顺序颠倒。 Note that the value 5 is not even present in the initial array. 请注意,初始数组中甚至都不存在值5 Additionaly, Array.Sort does not return anything so there would be nothing to pass to a using or while . Additionaly, Array.Sort不返回任何这样就不会有nothing要传递给usingwhile

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

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