Clear Filters
Clear Filters

How to sort rows?

2 views (last 30 days)
Noor Fatima
Noor Fatima on 12 Oct 2022
Commented: David Hill on 13 Oct 2022
>> A = [1 2; 4 3; 3 5; 2 1; 1 3; 4 5]
A =
1 2
4 3
3 5
2 1
1 3
4 5
>> B = sortrows(A)
B =
1 2
1 3
2 1
3 5
4 3
4 5
How to sort B, if entries of A are in java.math.BigInteger?
  1 Comment
Torsten
Torsten on 12 Oct 2022
uint64 as a MATLAB data type is not sufficient for your purpose ?

Sign in to comment.

Accepted Answer

David Hill
David Hill on 12 Oct 2022
import java.math.*
%load sample BigInteger Array (100x2)
for k=1:100
J(k,:)=[BigInteger(repmat(num2str(randi(1e14,1)),1,3)),BigInteger(repmat(num2str(randi(1e14,1)),1,3))];
end
%have to do sort manually, any sort algorithm will work but need to use
%compareTo() for BigIntegers
c=0;
while c~=size(J,1)-1
c=0;
for k=1:size(J,1)-1
if J(k,1).compareTo(J(k+1,1))<1
c=c+1;
else
temp=J(k+1);
J(k+1)=J(k);
J(k)=temp;
end
end
end
  3 Comments
Noor Fatima
Noor Fatima on 12 Oct 2022
@David Hill May I request for the following
The above code sort perfectly fine w.r.t first coordinate.
But if first coordinate is the same, it is not sorting w.r.t second coordinate. Like it can be done with sortrows in the above-mentioned B.
David Hill
David Hill on 13 Oct 2022
import java.math.*
%load sample BigInteger Array (100x2)
for k=1:100
J(k,:)=[BigInteger(repmat(num2str(randi(1e14,1)),1,3)),BigInteger(repmat(num2str(randi(1e14,1)),1,3))];
end
%have to do sort manually, any sort algorithm will work but need to use
%compareTo() for BigIntegers
c=0;
while c~=size(J,1)-1
c=0;
for k=1:size(J,1)-1
if J(k,1).compareTo(J(k+1,1))==0&&J(k,2).compareTo(J(k+1,2))==1
temp=J(k+1);
J(k+1)=J(k);
J(k)=temp;
elseif J(k,1).compareTo(J(k+1,1))<1
c=c+1;
else
temp=J(k+1);
J(k+1)=J(k);
J(k)=temp;
end
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!