Sorting a record by a user defined field
Author |
Message |
haayyeess
|
Posted: Sat Oct 31, 2015 1:30 pm Post subject: Sorting a record by a user defined field |
|
|
What is it you are trying to achieve?
I have a database program which currently only is holding a first and last name (in two different locations of a record). I want the user to be able to sort added data by ANY field (I will be adding more). Currently I have a sub receiving the users input into a temporary variable, but am not sure how to translate this choice into the record location/selection to sort within my sort routine. I have been trying to store the name of the record location within a variable, and using it but it's not working... Can anyone provide suggestions?
EX:
Record
First : string
Last : string
end record
Get FieldChoice from user...
FieldChoice := "First"
sort routine...
if Record (Comparision).FieldChoice ....
I even tried using a pointer, but was unable to find success. My computer science teacher hasn't used them in years and is unable to provide valid support...
What is the problem you are having?
When sorting, my sorting loop complains that the location I am trying to swap "is not a field of record".
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
procedure Sort_Initiation (var Person : PERSON_ARRAY, var RecordCount : nat1, var TempData : string)
%LOCAL VARIABLES
% Temp var to store data until proved valid
colour (brightgreen)
if RecordCount > 0 then %Data to sort
put "So you are looking to sort some of your records..."
put "Please enter the letter infront of the field you would like to sort"
put "Alternatively, you can type 'exit' to return to the main menu"
colour (white)
get TempData : *
if TempData = "exit" or TempData = "EXIT" then % Skip everything else and just exit
%Skip rest and exit to menu
elsif length (TempData ) = 1 and
(TempData >= "a" and TempData <= "b" or
TempData >= "A" and TempData <= "B") then %Valid Choice Made
%Decide which field is being sorted
%Re-assign TempData to whole field location of choice
if TempData = "A" or TempData = "a" then
TempData := "First"
elsif Choice = "B" or Choice = "b" then
TempData := "Last"
end if
Bubble_Sort (Person, RecordCount, TempData ) %Call bubble sort to sort
else
colour (brightred)
put "That wasn't a valid choice"
%Ignore, ask again.
end if
else %No Data to sort...
Font.Draw ("There are no records to sort!", 20, 175, Font1, brightred)
delay (1000) %Ensure error has been seen by user
loop %start loop 2 (Prevents delay time accumulation)
exit when not hasch % Exit when a correct key wasn't entered
getch (Choice )
end loop %end loop 2
end if
end Sort_Initiation
procedure Bubble_Sort (var Person : PERSON_ARRAY, var RecordCount : nat1, TempData : string)
%LOCAL TYPE DECLARATIONS
type RecordPointer : unchecked pointer to string %Points to record
%Short form could be RecordPointer : unchecked ^record
%LOCAL VARIABLES
%var TempData : string % Temp var to store data until proved valid
var TempSortHold : PERSON_RECORD_DATA %Record to hold data temp while swap
var Pointer : RecordPointer %Declarement of Pointer to point to record.
%GLOBAL VARIABLE ASSIGNMENTS
Pointer := cheat (RecordPointer, addr (TempData ))
%Sets address stored by pointer to be the address of a variable
%NOTE: cheat will only accept the type identifier to be a first parameter
%Short form of pointing could be #Pointer := addr (TempSortHold)
%To avoid issues with blank fields...
TempSortHold.First := ""
TempSortHold.Last := ""
%BUBBLE SORT
for Iteration : 1 .. RecordCount - 1
for Comparison : 1 .. RecordCount - 1
if Person (Comparison ).Pointer > Person (Comparison+ 1).Pointer then
%Data needs to swap, points to correct field to swap
%Swap ALL Record information
TempSortHold := (Comparison+ 1)
(Comparison+ 1) := (Comparison )
(Comparison ) := TempSortHold
end if
end for
end for
end Bubble_Sort
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Insectoid

|
Posted: Sun Nov 01, 2015 6:33 am Post subject: RE:Sorting a record by a user defined field |
|
|
As far as I know there isn't really a nice way to do this in Turing. You could use a pointer and have a condition in the loop that points it at either first or last name depending on what you want to sort, or you could swap to parallel arrays, which sucks but solves this problem pretty easily. |
|
|
|
|
 |
|
|