% ShowPop
% Data file StatePop.txt contains state names and state population.
% The states are listed in alphabetical order. 
% The states are then ranked by population and the results are written
% to the file StatePopOrdered.txt in that order

% Dump the contents of StatePop into a cell array...
C = File2Cell('StatePop');  % C{i} is the name and pop of state i
                                 % C{i} is a string

clc
% Extract the population data...
n = length(C);
pop = zeros(n,1); 
for i=1:n
  % pop(i) is the population of state i...
  State = C{i};
  pop(i) = str2double(State(16:24)); 
  fprintf('%s \n',State)
end

% Sort the states by population..
[s,rank] = sort(pop);

disp(' ')
% Set up a cell array of state strings...
Cnew = cell(n,1);
for i=1:n
    % Find the ith smallest state..
    ithSmallest = rank(i);
    State = C{ithSmallest};
    Cnew{i} = State;
    fprintf('%s \n',State)
end

Cell2File(Cnew,'StatePopOrdered')
