5 Replies - 30843 Views - Last Post: 23 April 2010 - 09:27 PM Rate Topic: -----

#1 Guest_Daniel*


Reputation:

How Collections.sort is doing it's stuff here?

Posted 19 April 2010 - 08:26 PM

I recently needed to sort an arrayList and found some code that supplied my needs, but I don't know exactly what is going on.

I wanted to order a bunch of "players" objects, each one has an attribute called points, this is the one that I wanted the sort method to work on. Here is the first part of the code:


public class Federation {
private ArrayList <Player> playerList = new ArrayList<Player>(); 
Collections.sort (playerList);
Collections.reverse (playerList);  }


In this class I just call Collections to sort the arrayList and then to revert the order

Now the rest:

public class Player implements Comparable <Player> {

private int rankingPosition;
	private Integer points;
	private String name;

public int compareTo(Player arg0) {
		int value = points.compareTo (arg0.points);
		return (value != 0 ? value : 1);
	}
} 


I don't understand what
Collections.sort
and
Collections.reverse
have to do with compareTo. compareTo is never called, so how is used?.
What means
points.compareTo
? points is just an attribute, how can it call a method?
And why is passing
arg0.points"
, if it should receive a player object?
I also don't fully understand
valor != 0 ? valor : 1)

It is checking if value is equal to zero, which means both objects were equal, but what it means the rest of the expression?

Thanks for the help.

Is This A Good Question/Topic? 0

Replies To: How Collections.sort is doing it's stuff here?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: How Collections.sort is doing it's stuff here?

Posted 19 April 2010 - 08:54 PM

Internally the Sort method does call CompareTo of the classes it is sorting. How does a sort work? It takes two items, determines which one is greater and swaps them if need be. When you call Collections.Sort and give it a collection, it goes to the list and takes two items. It then calls compareTo on one of them and gives it the second. It asks "Which is greater?" CompareTo returns -1, 0 or 1 to say if it is less than, equal, or greater to the other. It uses this result to then determine if they should be swapped for its sort. After it is done, it takes another two elements and does the whole process again. It does this until the collection is sorted.

Assume we have a list that has Player objects in it. Player1, Player2 and Player3. Player1 has more points than Player2. Sort takes Player1, calls its compareTo and gives it Player2 as a parameter (that is why it takes a parameter of type Player). It compares the two using the points as a way to determine equality. It returns -1 saying Player2 has fewer points than Player1. The Sort then knows it needs to swap Player1 and Player2. It then takes Player2 and calls its compareTo method giving it Player3 to compare.

Hope you understand the process now. That is why all objects in a collection must implement compareTo if it wishes to be sortable using Collection.Sort. Sort needs to have compareTo to determine equality to be able to sort properly.

:)
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: How Collections.sort is doing it's stuff here?

Posted 19 April 2010 - 09:10 PM

Quote

CompareTo returns -1, 0 or 1 to say if it is less than, equal, or greater to the other.

Actually, as long as compareTo() returns a negative, 0, or positive int, then the sort will work fine. The sort() method (along with most standard Java libary methods that rely on Comparable or Comparator interfaces) check for positive and negative values, not -1 & 1 specifically as the API suggests.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Daniel*


Reputation:

Re: How Collections.sort is doing it's stuff here?

Posted 20 April 2010 - 11:12 PM

Thanks to both. Still have some doubts

points.compareTo (arg0.points);


I imagine that what can be happening is something like:
player0.getPoints().compareTo (arg0.getPoints())


Can be this? And the original piece is a shortcut for what I wrote?

also:
valor != 0 ? valor : 1


Can you guys talk about the purpose of the "?" and ":"

BTW: Can you pass me a link so that I can register? I did not see one.

Thanks in advance
Was This Post Helpful? 0

#5 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: How Collections.sort is doing it's stuff here?

Posted 21 April 2010 - 02:43 AM

View PostDaniel, on 21 April 2010 - 06:12 AM, said:

I imagine that what can be happening is something like:
player0.getPoints().compareTo (arg0.getPoints())


Exactly. But because the compareTo method is part of that class, it can access private fields directly.

Quote

valor != 0 ? valor : 1
Can you guys talk about the purpose of the "?" and ":"


It's basically equivalent to this:

if (valor != 0) {
  return valor;
} else {
  return 1;
}


But what I can't work out is why that is a desirable thing to do. Basically what it means is:

If one is bigger return the result.
But if they are the same, pretend one is bigger.

Unless I've interpreted it wrongly. :S
Was This Post Helpful? 0
  • +
  • -

#6 Guest_daniel*


Reputation:

Re: How Collections.sort is doing it's stuff here?

Posted 23 April 2010 - 09:27 PM

Quote

valor != 0 ? valor : 1
Can you guys talk about the purpose of the "?" and ":"


It's basically equivalent to this:

if (valor != 0) {
  return valor;
} else {
  return 1;
}


But what I can't work out is why that is a desirable thing to do. Basically what it means is:

If one is bigger return the result.
But if they are the same, pretend one is bigger.

Unless I've interpreted it wrongly. :S
[/quote]

Yes, I also doesn't understand why.

I thought about doing:
if (valor < 0)
        return -1;
      else if (valor > 0)
	return 1;


Then my IDE keep telling me:

Quote

-This method must return a result of type int

Soon after I realised that valor already should be with the correct value, from the line
int valor = points.compareTo (arg0.points);
, a negative, zero or a positive one, so all I did was to put
return valor
soon after
int valor = points.compareTo (arg0.points);
and then close the method. It worked as I expected, but I also have no idea why the above piece works
Was This Post Helpful? 0

Page 1 of 1