I know that the code sorts a list of temperatures numerically but i cant get my head round what it does in detail, as in line by line.

public void sortnumerically()
	{
		for (int i = 0; i < temperatures.size() - 1; i++)
		{
			int min = i;

			for (int current = i + 1; current < temperatures.size(); current++)
			{
				if (temperatures.get(current) < temperatures.get(min))
				{
					min = current;
				}

			}

			temperatures.set(i, temperatures.get(min));
			
			cities.set(i, cities.get(min));

			temperatures.set(min, temperatures.get(i));
			
			cities.set(min, cities.get(i));
		}

Recommended Answers

All 3 Replies

Put this chunk of code in a main inside class, compile it, then run it, add a System.out.println() after every statement to trace what is happening to your variables.
Thats the most optimal way of finding out rather than asking people for a line by line explanation.

better yet ... make sure you have the complete code

for instance: what is cities? performing actions on something you don't know ...
can be tricky :)

This gives me the feeling that this code is not written by you, can you tell us why so ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.