d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How Would I Sort An Array Of Objects?
12Next
Add Reply New Topic New Poll
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 6 2013 07:24pm
I need to sort an array of objects based on a % score each object has.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2013 07:32pm
Google "java sort comparator"
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 6 2013 07:42pm
Quote (carteblanche @ Nov 6 2013 08:32pm)
Google "java sort comparator"


We haven't covered that and I am not allowed to use that.

He mentioned something about using a get method and using a bubble sort, but I'm not sure how to go about this.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 6 2013 08:00pm
Quote (GetSpanked @ Nov 6 2013 08:42pm)
We haven't covered that and I am not allowed to use that.

He mentioned something about using a get method and using a bubble sort, but I'm not sure how to go about this.


What part about it is confusing you?

Post your code and what problems you are having

This post was edited by Eep on Nov 6 2013 08:00pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2013 08:29pm
Quote (GetSpanked @ Nov 6 2013 09:42pm)
We haven't covered that and I am not allowed to use that.

He mentioned something about using a get method and using a bubble sort, but I'm not sure how to go about this.


how about compareTo for the Comparable interface?

here's some pseudocode for bubble sort to get you started

Code
procedure bubbleSort( A : list of sortable items )
repeat
swapped = false
for i = 1 to length(A) - 1 inclusive do:
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 6 2013 09:38pm
Why is this not working?
Code
public double bubbleSort(Student[] students) {
for (int a=1; a<students.length; a++) {
for(int b=0; b<students.length - a; b++) {
if (((students[b].getOverAllPercentage()) - ((students[b+1].getOverAllPercentage()))) < 0)
//swap movies[b] with movies[b+1]
temp = students[b];
students[b] = students[b+1];
students[b+1] = temp;
}
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 6 2013 09:46pm
you likely need braces around your if block.

the indentation should have been a clue. and you need to learn how to debug.

This post was edited by carteblanche on Nov 6 2013 09:47pm
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 6 2013 09:57pm
Quote (carteblanche @ Nov 6 2013 10:46pm)
you likely need braces around your if block.

the indentation should have been a clue. and you need to learn how to debug.


I added the braces but how do i call this method from the student class?
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 6 2013 09:58pm
my main method is in another class than the student class which is where this bubble sort is at. I need to call this method in my main class so that it will sort my array of student objects.
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 6 2013 09:59pm
I also changed the code to this.

Code
public void bubbleSort(Student[] students) {
for (int a=1; a<students.length; a++) {
for(int b=0; b<students.length - a; b++) {
if (((students[b].getOverAllPercentage()) - ((students[b+1].getOverAllPercentage()))) < 0){
Student temp = students[b];
students[b] = students[b+1];
students[b+1] = temp;
}
}
}
}
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll