Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:03:11+00:00 2026-05-25T16:03:11+00:00

Basically, I have data of several people contained in a .csv file. This spreadsheet

  • 0

Basically, I have data of several people contained in a .csv file. This spreadsheet file contains the name of people in one column, followed by their age and work experience in two other columns.

So far I have correctly managed to display the data in a text area. However, I wish to be able to organise the people alphabetically by their name, or by their age in ascending order when the user clicks one of two buttons. For this example, I have attempted to limit the complexity by just trying to sort by age.

I have been told in order to do this, I should use a selection sort algorithm. Unfortunately, I have only used selection sort with arrays, not arrayLists, and not with data stored in a .csv file.

The problem that I have encountered is that I do not how to go through the data (for arrayLists), and then reassign the minimum position to the lowest number. Please refer to the minimumPosition method.

 public class Inputs extends JFrame
 {
   ArrayList <People> pList = new ArrayList <People>();
   JButton   NameSortButton;
   JTextArea DisplayTextArea; 

String outputText = "";

public Inputs()
{
        // Construct the GUI
      class innerListener implements ActionListener
    {
        public void actionPerformed (ActionEvent myActionEvent)
        {
            if (myActionEvent.getSource() == AgeSortButton)
            {
                sortByAge();
            }
        }
    }

    ActionListener inListener = new innerListener();
    NameSortButton.addActionListener(inListener);
}



public void readPeopleData()
{
    FileReader reader = null;
    int lineNumber = 1;

    try
    {
        reader = new FileReader("People.csv");
        Scanner in = new Scanner(reader);

        while (in.hasNextLine())
        {
            String input = in.nextLine();
            String section[] = input.split(",");
            pList.add(new People(section[0], section[1], Integer.parseInt(section[2]), Integer.parseInt(section[3])));
            lineNumber++;
        }

        for (People p: pList)
        {
            String heading = "Name \tAge \tWork Experience";
            outputText     = outputText + p.getPersonName()  +
                                  "\t"  + p.getAge()         +
                                  "\t"  + p.getExperience();
            DisplayTextArea.setText(heading + outputText);
        }
    }

    catch(IOException error)
    {
        JOptionPane.showMessageDialog(null, "File not Found","Error" , JOptionPane.ERROR_MESSAGE );
    }
} //    public void readPatientData()

private void sortByAge()
{
    for (int i = 0; i < pList.size(); i++)
    {
        int minPos = minimumPosition(i);
        swap(minPos, i);
    }
    displayOutputs();

}

private int minimumPosition(int from)
{
    int minPos = from;

    for (int i = from + 1; i < pList.size(); i++)
    {
                        // ****** PROBLEM AREA ****** //
        // if (pList.get(i).getAge() < // NO IDEA)
                          {
               minPos = i;
           }

                       /* 
                       The text book sorted arrays by: 
                       for (int i = from + 1; i < array.length; i ++)
                             if (array[i] < array[minPos])
                              {
                                   minPos = i;
                              } 
                       */

    }
    return minPos;
}


private void swap(int i, int j)
{
    People temp = pList.get(i);
    pList.set(i, pList.get(j));
    pList.set(j, temp);
}

private void displayOutputs ()
{
    for(int j = 0; j < pList.size(); j++)
    {
         outputText = outputText    + pList.get(j).getPersonName()     +
                               "\t" + pList.get(j).getAge()         +
                               "\t" + pList.get(j).getExperience()  +

        DisplayTextArea.setText(outputText);
        System.out.println(outputText);
    }
}

public static void main (String args[])
{
} //    public static void main (String args[])

}

Can somebody please point me in the right direction? How can I compare the sorted data to the unsorted data. Furthermore, are my for loops correct?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:03:11+00:00Added an answer on May 25, 2026 at 4:03 pm

    Basically you could treat ArrayList like an array, since it is a list backed by an array.

    You’re already on a good way with you implementation.

    Instead of this:

    for (int i = from + 1; i < array.length; i ++) {
      if (array[i] < array[minPos]) {
        minPos = i;
      } 
    }
    

    You’d replace the array access with list operations:

    for (int i = from + 1; i < pList.size(); i ++) {
      if ( pList.get(i).getAge() < pList.get(minPos ).getAge() ) {
        minPos = i;
      } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two sources: an xml-file containing definitions of several hundred data structures, basically
I basically have three tables, posts, images and postimages (this simply contains the ids
I have a several C# console applications that basically parse tons of data in
I have an Entity which is basically one Datamodel. That Model has several Columns
I have 2 tables...they are basically the same except for the column name in
Possible Duplicate: Xcode: Storyboard Tabbed Application Passing Data Back and Forth Basically I have
I have a nice DataGridView showing what is basically some kind of log data
I have an application that shows data from a MySQL table. Basically, my application
I have a few modules, but I will focus on one in particular. This
I have a data frame with several rows and I want to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.