Sorting a BigInteger array

I need to sort an array but the contents are BigIntegers. When I try use the "sort(A)" function I get the following error:
Error using sort
Unknown command option.
Is it because the array is type BigInteger[] ? And if that's the case, how can I sort it?

 采纳的回答

Jan
Jan 2022-4-29

0 个投票

How dod you create the array? Please post a short example. I seems that you are talking about a Java class, not a Matlab array. Then use a Java method for sorting...
FEX: HPF might be useful. It has implemented an merge sort.

4 个评论

I have a for loop that uses a formula to generate the numbers. They were too late to be normal integers so I had to use java.math.* to use BigIntegers within matlab.
z_n = @(n) x_n(n).subtract(y_n(n)).mod(m1);
for i=1:1000
Z(i) = z_n(i);
end
Your code example does not contain any "java.math.BigInter". What is y_n and m1?
Please post a minimal working example, to reduce the need to guess, what you are doing.
With bold guessing:
a = java.util.ArrayList;
for k = 1:5
v = randi([0, 2^16]);
a.add(java.math.BigInteger(typecast(v, 'uint8')));
end
a
a = [542892608, 9623872, 12368960, 2154949440, 3235961408]
java.util.Collections.sort(a)
a
a = [9623872, 12368960, 542892608, 2154949440, 3235961408]
I have tried what you wrote in your comment and I get the following :
No method
'java.util.Collections.sort' with
matching signature found.
I imported java.util.* but that doesn't change the outcome.
@Michelle Lear: The link does not uniquely explain, how you have defined your array of BigIntegers. It would be helpful if you post your code instead of letting me guess the details.
I've showed you the code to inster the BigIntegers in a java.util.ArrayList, and this is a valid input for java.util.Collections.sort. If you use the suggested method, you will not get this error message.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品

版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by