简体   繁体   English

使用Array.sort()对字节数组进行排序

[英]Sort byte array using Array.sort()

Really stupid question, but I'm a bit stuck on this one: 真的很愚蠢的问题,但我有点卡在这一个:

byte[] bytes = new byte[]{1, 2, 3};
Arrays.sort(bytes, (byte a, byte b) -> 0);

Why isn't the lambda matching any methods of Array.sort and how can I fix it? 为什么lambda不匹配Array.sort任何方法,我该如何解决?

I guess you're trying to use Arrays.sort(T[] a, Comparator<? super T> c) , so the problem is that byte isn't a T , because it's a primitive type. 我猜你正在尝试使用Arrays.sort(T[] a, Comparator<? super T> c) ,所以问题是byte不是T ,因为它是一个原始类型。 I guess this would work with Byte instead. 我想这可以与Byte使用。

There are no sort methods accepting an array of primitive values and a Comparator . 没有sort方法接受原始值数组 Comparator Only arrays of objects can be sorted with a comparator unfortunately. 遗憾的是,只能使用比较器对对象数组进行排序。

Try 尝试

Byte[] bytes = new Byte[] { 1, 2, 3 };
Arrays.sort(bytes, (Byte a, Byte b) -> 0);

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

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