How to sort a String Array in MATLAB Script?

This video shows how to write a simple matlab script to sort a string array in alphabetical order in matlab.


We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source code:

% A = [“God”; “Is”; “Great”; “Universe”; “Is”; “Big”];
A = [“God”; “AAA”; “Shivpurkar”; “Holicks”; “Bertha”; “Adams”];

Sorted_Array = A(1);

for i = 2:length(A)
shift = 0;
for j = 1:length(Sorted_Array)
if(A(i) < Sorted_Array(j))
shift = 1;
break;
end
end

if (j==1)
if(shift == 1)
Sorted_Array = [A(i); Sorted_Array];
else Sorted_Array = [Sorted_Array; A(i)];
end
else
if(shift == 1)
Sorted_Array = [Sorted_Array(1:j-1); A(i); Sorted_Array(j:end)];
else Sorted_Array = [Sorted_Array; A(i)];
end

end
end

Sorted_Array

Leave a Reply

%d bloggers like this: