I’m trying to sort a two dimensional array based on an arbitrary column like this:
order_organizer.adb
with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;
with Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Wide_Unbounded; use Ada.Strings.Wide_Unbounded;
with PragmARC.Line_Fields;
with Sorting_Tools;
procedure Order_Organizer is
-- User input value to select a given column of data in the two-dimensional array
i : Integer := 1;
Num_Orders_In_Order_Array : Integer := 0;
Index, Starting_Point, Current_Index : Integer := 1;
Header_String, Order_Entry_String : Unbounded_String;
In_File_Name : String := "MegaOrder_dummy.txt";
Out_File_Name : String := "MegaOrder_new.txt";
Header_List, Parsed_Field_List : PragmARC.Line_Fields.Field_List;
Order_File_Handle, New_Order_File_Handle: File_Type;
Mega_Order_Array : Sorting_Tools.Order_Entry_Type (Integer range 1 .. Sorting_Tools.Max_Order_Size);
begin
-- Open the MegaOrder file.
Open (Order_File_Handle, Mode => In_File, Name => In_File_Name);
-- Read and strip out the the header.
Header_List := PragmARC.Line_Fields.Parsed (Get_Line (Order_File_Handle), '|');
while not End_Of_File (Order_File_Handle) loop
Parsed_Field_List := PragmARC.Line_Fields.Parsed (Get_Line (Order_File_Handle), '|');
Mega_Order_Array (Index)(1) := Parsed_Field_List (1);
Mega_Order_Array (Index)(2) := Parsed_Field_List (2);
Mega_Order_Array (Index)(3) := Parsed_Field_List (3);
Index := Index + 1;
end loop;
Num_Orders_In_Order_Array := Index-1;
Sorting_Tools.Sort_By_Index (Mega_Order_Array);
--Sorting_Tools.Sort_By_Index (Mega_Order_Array, i);
Current_Index := Starting_Point;
Create (New_Order_File_Handle, Out_File, Out_File_Name);
Put_Line (New_Order_File_Handle, "orderName|orderStatus|orderQuantity");
for Index in 1 .. Num_Orders_In_Order_Array loop
Put_Line (New_Order_File_Handle, To_Wide_String (
Mega_Order_Array (Current_Index)(1)
& "|" & Mega_Order_Array (Current_Index)(2)
& "|" & Mega_Order_Array (Current_Index)(3)));
Current_Index := Current_Index + 1;
end loop;
Close (New_Order_File_Handle);
Put_Line ("End of program.");
end Order_Organizer;
sorting_tools.adb
with Ada.Strings.Wide_Unbounded; use Ada.Strings.Wide_Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
package body Sorting_Tools is
function Index_Asc (L, R : Order_Entry_Array) return Boolean is
--function Index_Asc (L, R : Order_Entry_Array; i: Integer) return Boolean is
begin
--return L(1) < R(1);
return L(3) < R(3);
--return L(i) < R(i);
end Index_Asc;
end Sorting_Tools;
sorting_tools.ads
with Ada.Strings.Wide_Unbounded;
with Ada.Containers.Generic_Array_Sort;
with Ada.Wide_Characters;
with Ada.Wide_Text_IO;
package Sorting_Tools is
type Order_Entry_Array is array (1 .. 5) of Ada.Strings.Wide_Unbounded.Unbounded_Wide_String;
type Order_Entry_Type is array (Natural range <>) of Order_Entry_Array;
Max_Order_Size: constant Integer := 6;
function Index_Asc (L, R : Order_Entry_Array) return Boolean;
-- function Index_Asc (L, R : Order_Entry_Array; i : Integer ) return Boolean;
procedure Sort_By_Index is new Ada.Containers.Generic_Array_Sort
(Natural, Order_Entry_Array, Order_Entry_Type, "<" => Index_Asc);
end Sorting_Tools;
MegaOrder_dummy.txt
orderName|orderStatus|orderQuantity
"name13"|"good"|3
"name5"|"good"|6
"name1"|"missing"|4
"name5"|"good"|2
"name6"|"best"|1
"name2"|"good"|44
MegaOrder_new.txt
orderName|orderStatus|orderQuantity
name6|best|1
name5|good|2
name13|good|3
name1|missing|4
name2|good|44
name5|good|6
The problem I’m having is that I don’t know how to pass a column index into the sorting function to tell the generic array sort library to sort on a designated column (e.g. 1, 2, 3). Maybe I need to use a different sorting algorithm. I just don’t want to have to write 50 different function definitions for each column if I had, say, 50 columns in the array I was trying to sort. Note how I have lines of code commented out for how I wish this would work, based on a user-inputted column index.