inpfid = fopen('InputFile.txt'); %This .txt file contains the data you gave in the question
allData = textscan(inpfid,'%s','Delimiter','\n');
% Read in the first word from each row of data
outcellarray = regexp(allData{:},'^([\w\-]+)','match');
% Store all the first numbers into a single cell array and sort them
[~, ind] = sort(str2double(vertcat(outcellarray{:})));
% Creating a cell with the required order
output = cellfun(@(x) x(ind), allData, 'UniformOutput', 0);
% Making it into a form useable for writing a text file
output= output{:};
outfid=fopen('OutputFile.txt','wt+'); %Creating an output file
for k = 1:length(output)-1
%Writing the data
fprintf(outfid,output{k});
fprintf(outfid,'\n');
end
fprintf(outfid,output{end});
% You can loop from 1 to length(output) and skip the last line
% but it'll append an extra line at the end of the output file
fclose(outfid); % Closing the output file
Vergleich von Ein- und Ausgabedateien:
PS:
✶ Machen Sie es sicher, dass sowohl Ein- und Ausgabedateien an Ihrem aktuellen Pfad oder Stellen Sie vollständige Pfade wie: 'D: \ Assignment \ InputFile.txt'
✶ I used/kopiert den Code/Ideen aus:
①https://stackoverflow.com/a/23357800/5698672
②https://stackoverflow.com/a/5041474/3293881
③https://stackoverflow.com/a/28348768/5698672
so dass diese Antworten eine Stimme geben, wenn es Ihr Problem löst.
Ps ich interessiere mich nicht für Geschwindigkeiten, nur der einfachste Code – JohnnyF
Für die Zukunft vermeiden Sie bitte, etwas mit einer irrelevanten Sprache zu markieren. –
ja ich tat, ich tat es mit Indexen der benutzten Linien behalten und gerade über die Akte immer wieder laufen, (und tun intcept) aber dieses ist rly hässlicher Code und ich hoffe, dass jemand gimme abatter Idee, ich will nicht gerade einen Code eine richtige Art darüber nachzudenken – JohnnyF