• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting  RSS feed

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code below, I'm trying to sort my data, but the final data piece isn't being capture in my sort method. How can I fix this?

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:


This sort() is a bit weird. First, it creates an iterator, which it doesn't use at all. Second, it sorts the list as many times as the size of the list -- meaning if there are 10 items in the list, it will sort the list 10 times. Since, sorting an already sorted list doesn't really change anything, all but the first sorting is kinda useless.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:


This reverse() method doesn't actual reverse anything -- it only prints it. The list remains unchanged after the method call.

Henry
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:For the code below, I'm trying to sort my data, but the final data piece isn't being capture in my sort method. How can I fix this?

Like Henry, I'm a bit confused by your logic. For one thing, I think you're confusing sorting with ordering and iteration, and the three things are totally separate.

A List's order is determined by its elements (usually "insertion order") which may not be predictable, but is always the same. Now, whether or not they are "sorted" is usually determined by some other criteria that you decide, either by making those elements Comparable, or by supplying a Comparator, to a sort method.

An Iterator, on the other hand, simply traverses elements in whatever order they happen to be, starting at element 0 and continuing to element size()-1 (or in reverse sequence if you use ListIterator's "reverse" methods). If the List is "sorted" then they will be returned in that order, but the act of iteration is entirely separate from the business of sorting.

Not sure if it helps, but hopefully it does.

Winston

PS: I dislike methods that return (or rather don't return) void. With a few notable exceptions, methods should return something; you just have to decide what it is. I suspect this is also part of your problem.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. For the reverse, per our instructions, we only have to print it in reverse, we don't actually need to reverse the whole list.

Could you give me an example of how my reverse() method my look if it had a return. We've been using plenty of void methods in my class and plenty of non-void ones, so this is a design idea that I'm unfamiliar with.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my sort method, but it still doesn't work of course. How can I think this through. I understand what you guys are saying, and I'm going through the APIs you linked, but I'm not sure how to wrap my head around this. Basically, what I know I need to do is:

1. Iterate through the list
2. As I go through the list check to see if n2 is greater than or less than n1 and adjust their placement accordingly.
3. Keep doing this until I get to the end.

So I'm not sure whether I should use Collections.sort(), or if I should use some other method. Maybe there's a way to apply this.

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don'lt edit your posts like that. It makes the replies look like nonsense. Fortunately I can restore them from the replies you have had.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:
Whatever you put when you edited the method, will not work either. That looks like code where you are guessing, and your guessing is definitely directed … towards the wrong code.
Are you supposed to use Collections#sort or write your own sorting method?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We aren't given direction either way.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:We aren't given direction either way.



Perhaps you should get clarification as ... using a sorting algorithm is much much easier than writing your own ... but if you use it, and the homework assignment is to write one, you will fail the project.

Henry
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given how things have been historically, when we aren't given direction on something, we are free to solve the problem in whatever way seems best to us. We have written our own algorithms and have used Collections.sort() in the past. Basically, I need to figure out the easiest solution to the problem, but I'm an unsure how to think it through correctly.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check carefully, but if you have not been given any direction either way, Collections#sort is probably the better approach. If you look in Joshua Bloch's Effective Java™, there is a section saying always to use library classes when they are available.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not have access to that book. Is there another way I could be guided through the process?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Check carefully, but if you have not been given any direction either way, Collections#sort is probably the better approach. If you look in Joshua Bloch's Effective Java™, there is a section saying always to use library classes when they are available.


Sam Pauken wrote:I do not have access to that book. Is there another way I could be guided through the process?



Not sure what you mean by process -- what Campbell pointed to is common sense. Use something that has been tried and tested, versus something that is new and is being used for the first time.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!