Note that there are some explanatory texts on larger screens.

plurals
  1. POSelection sort - arrayLists
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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. </p> <p>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 <em>minimumPosition</em> method.</p> <pre><code> public class Inputs extends JFrame { ArrayList &lt;People&gt; pList = new ArrayList &lt;People&gt;(); 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 &lt; 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 &lt; pList.size(); i++) { // ****** PROBLEM AREA ****** // // if (pList.get(i).getAge() &lt; // NO IDEA) { minPos = i; } /* The text book sorted arrays by: for (int i = from + 1; i &lt; array.length; i ++) if (array[i] &lt; 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 &lt; 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[]) </code></pre> <p>} </p> <p>Can somebody please point me in the right direction? How can I compare the sorted data to the unsorted data. Furthermore, are my <em>for loops</em> correct?</p> <p>Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload