/    Sign up×
Community /Pin to ProfileBookmark

Sorting two-dimensional arrays

I am using a two-dimensional array that contains info about patients, and another one about diagnoses. What I’m trying to do is link a certain patient with a diagnosis. The problem appears when the patient is not listed in my PatientsDB array database: I wan’t the new patients to be displayed alphabetically in the select form ‘patients’ and if possible, the latest entered element to be selected. I tried to sort the PatientDB array first, but without any success.

  • 1.

    Is there a way to sort and display the options in the select form by patient name, id, address, etc.?

  • 2.

    Is there an alternative way to add new patient info?

  • 3.

    Can the newly generated PatientsDB array be saved for further reference when I close the hml document?

  • Thank you for your attention.

    <html>
    <head>
    <title>dynamically sort the options in a select form</title>
    <meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-5″>
    <SCRIPT LANGUAGE=”JavaScript1.1″>
    <!– start script
    // stuff “rows” of data for our pseudo-two-dimensional array
    function patient(name,PatientID, town, address, day){
    this.name = name
    this.PatientID = PatientID
    this.town = town
    this.address = address
    this.day = day
    }
    // create our pseudo-two-dimensional array
    PatientsDB = new Array()
    PatientsDB[0] = new patient(“Adam”,”00272″,”h”,”i”,”20.07″);
    PatientsDB[1] = new patient(“Brian”,”00351″,”g”,”j”,”20.06″);
    PatientsDB[2] = new patient(“Carl”,”15764″,”f”,”k”,”20.05″);
    PatientsDB[3] = new patient(“Donald”,”05786″,”e”,”l”,”20.04″);

    // stuff “rows” of data for our pseudo-two-dimensional array
    function diagnose(diagnose, diagnosecode){
    this.diagnose = diagnose
    this.diagnosecode = diagnosecode
    }
    // create our pseudo-two-dimensional array
    DiagnosesDB = new Array()
    DiagnosesDB[0] = new diagnose(“Acute Enterocolitis”,”009″)
    DiagnosesDB[1] = new diagnose(“Acute Tracheobronchitis”,”466.0″);

    // Populating the PatientsDB array with new data
    function NewPatient(){
    var NewName = prompt(“Patient Name: “,””);
    var NewPatientID = prompt(“Patient ID: “,””);
    var NewTown = prompt(“Town: “,””);
    var NewAddress = prompt(“Address: “,””);
    var NewDay = prompt(“Date (dd.mm): “,””);
    PatientsDB[PatientsDB.length] = new patient(NewName,NewPatientID,NewTown,NewAddress,NewDay);

    PatientsDB.sort()

    var page = “” // start assembling next part of page and form
    page += “<SELECT NAME=’patients’> “
    // build popup list from array patient names
    for (var i = 0; i < PatientsDB.length; i++) {
    page += “<OPTION” // OPTION tags
    if (i == 0) { // pre-select first item in list
    page += ” SELECTED”
    }
    page += “>” + PatientsDB[i].name
    }
    page += “</SELECT><P>” // close selection item tag
    document.getElementById(‘PatientsList’).innerHTML = page;
    }

    // end script –>
    </SCRIPT>

    <BODY>

    <H1><a href=”#” onClick=
    “NewPatient();
    return false;”>Enter new patient data</a> </H1>
    <HR>

    <H1><a href=”#” onClick=
    “alert(‘Patient Name: ‘ + PatientsDB[window.document.select.patients.selectedIndex].name);
    alert(‘Patient ID: ‘ + PatientsDB[window.document.select.patients.selectedIndex].PatientID);
    alert(‘Diagnose: ‘ + DiagnosesDB[window.document.select.diagnose.selectedIndex].diagnose);
    alert(‘Code of the Diagnose: ‘ + DiagnosesDB[window.document.select.diagnose.selectedIndex].diagnosecode);
    return false;”>View the selected patient data</a></H1>
    <HR>
    <FORM name=”select”>
    <SCRIPT LANGUAGE = “JavaScript”>
    <!– start script again
    var page = “” // start assembling next part of page and form
    page += “Select a patient: “
    page += “<div id=’PatientsList’><SELECT NAME=’patients’> “
    // build popup list from array patient names
    for (var i = 0; i < PatientsDB.length; i++) {
    page += “<OPTION” // OPTION tags
    if (i == 0) { // pre-select first item in list
    page += ” SELECTED”
    }
    page += “>” + PatientsDB[i].name
    }
    page += “</SELECT></div><P>” // close selection item tag
    document.write(page) // lay out this part of the page

    var pagedg = “” // start assembling next part of page and form
    pagedg += “Select a patient’s diagnose: “
    pagedg += “<SELECT NAME=’diagnose’> “
    // build popup list from array patient names
    for (var i = 0; i < DiagnosesDB.length; i++) {
    pagedg += “<OPTION” // OPTION tags
    if (i == 0) { // pre-select first item in list
    pagedg += ” SELECTED”
    }
    pagedg += “>” + DiagnosesDB[i].diagnose
    }
    pagedg += “</SELECT><P>” // close selection item tag
    document.write(pagedg) // lay out this part of the page

    // really end script –>
    </SCRIPT>

    </FORM>
    </BODY>
    </HTML>

    to post a comment
    JavaScript

    0Be the first to comment 😎

    ×

    Success!

    Help @Vassil_Catsarov spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...