# Help with array List

Elvis Ve
Greenhorn
Posts: 19
hey whats up guys, Im having a problem I created a sort method in order to organize the lastNames by alphabetical order here is my sort method: why is it not working when i call the sorth method

void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{

Student s= (Student)list.get(i);
Student p= (Student)list.get(startScan);
Student temp = s;
s = p;
p = temp;

}

int findPosition(int i)
{
int startScan = i;
Student s = (Student)list.get(i);
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
k = startScan;

}
return startScan;
}

chi Lin
Ranch Hand
Posts: 348
What is the problem ?

Is the sorted result not 100% correct or nothing is working ?

for(int k =0; k < list.size()-1;k++), looks like the -1 here could cause a boundary problem.

Elvis Ve
Greenhorn
Posts: 19
its not sorting at all, i dont know if i have something wrong inside but everything seems to be right

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34641
363
Elvis,
The problem lies in the swap method. At the end of the method, you have swapped the variables that "s" and "p" are pointing to. However, you did not set the values back to the array. So the array has the same values and the swap is not occuring.

chi Lin
Ranch Hand
Posts: 348
inside findPosition(int i), looks to me the i passed into the
findPosition() was returned again.

(i assign to startScan, startScan then returned without any change)

Nick George
Ranch Hand
Posts: 815
just a word of warning... cross posting tends to be frowned upon...

Marilyn de Queiroz
Sheriff
Posts: 9064
12

Elvis Ve
Greenhorn
Posts: 19
I made some fixes to it but for some reason it still wont sort, check it out: what is wrong with it?, this is driving me crazy, please help and thanks in advance

void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{

Student s = (Student)list.get(i);
list.set(i, list.get(startScan));
list.set(startScan, s);

}

int findPosition(int i)
{
int startScan = i;
Student s = (Student)list.get(i);
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
startScan = k;

}
return startScan;
}

Elvis Ve
Greenhorn
Posts: 19
by the way here is my test class just in case i made a mistake there and the sorting is not working because of this:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

class Driver
{
public static void main(String arg[])
{

Filing f = new Filing();

int m;

do
{
m = getData.getInt("Choose from the follwing menu:\n1- add a Student without GPA. \n2- add a Student with GPA. \n3- change a Student's current GPA\n4-Print\n5-Exit");

switch(m)
{
case 1:
String id = getData.getString("enter student's id");
String lName = getData.getString("enter student's last name");
String fName = getData.getString("enter student's first name");
Student s = new Student(id,lName,fName);

break;
case 2:
String Id = getData.getString("enter student's id");
String LName = getData.getString("enter student's last name");
String FName = getData.getString("enter student's first name");
double gPA = getData.getDouble("enter the student's GPA");
Student t = new Student(Id,LName,FName,gPA);
break;
case 3:
String tempID = getData.getString("enter student's id");
double NewGPA = getData.getDouble("enter the student's GPA");
if(f.search(tempID) == true)

break;
case 4:
f.Sort();
JTextArea text = new JTextArea(f.toString(),15,40);
JScrollPane scroll = new JScrollPane(text);
JOptionPane.showMessageDialog(null, scroll,"Student Information", JOptionPane.INFORMATION_MESSAGE);
break;
case 5:
break;
default:
break;

}

}
while(m!=5);

}
}

Dirk Schreckmann
Sheriff
Posts: 7023
Elvis,

This isn't the best forum around here for this conversation.

I'm moving this to the Intermediate forum.

Elvis Ve
Greenhorn
Posts: 19
anybody?..... this sort thing is really making me mad

fei long
Ranch Hand
Posts: 48
I am not quite sure how would you like to sort. But I noticed that you might have to change
-------------------------------------
int startScan = i;
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
startScan = k;
----------------------------------------
k=0 --> k=i
in findPosition()