Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to sort an array of strings where each string represents a decimal number in Pascal

1 Answer

0 votes
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  

*)

 



answered Sep 1, 2025 by avibootz
...