program SortDecimalStrings;
uses
SysUtils; // StrToFloat
const
// Input array of strings
N = 8;
numbers: array[1..N] of string = (
'12.3', '5.6', '789.1', '3.14', '456.0', '0', '0.01', '4.0'
);
var
sorted: array[1..N] of string;
i, j: integer;
temp: string;
numA, numB: Double;
// Comparator logic: sort strings as decimal numbers
begin
// Copy original array to sorted array
for i := 1 to N do
sorted[i] := numbers[i];
// Sort the array using a simple bubble sort and custom comparator
for i := 1 to N - 1 do
for j := i + 1 to N do
begin
numA := StrToFloat(sorted[i]);
numB := StrToFloat(sorted[j]);
if numA > numB then
begin
temp := sorted[i];
sorted[i] := sorted[j];
sorted[j] := temp;
end;
end;
// Output the sorted array
writeln('Sorted array of decimal strings:');
for i := 1 to N do
write(sorted[i], ' ');
writeln;
end.
(*
run:
Sorted array of decimal strings:
0 0.01 3.14 4.0 5.6 12.3 456.0 789.1
*)