• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort an Array  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to sort my game array but am at a loss. I have looked all over the internet and can't find anything to help, nor understand what I am finding. Please help, the code that works for this plus an explanation would be awesome!


 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want Arrays.sort to sort your data, but what should be the basis of that sorting?
Data you want to be sorted is defined by you and hence you also need to define the criteria for its
sorting which you do either by implementing Comparable interface for your data class(Gamedetail here) OR
by writing a class which implements Comparator interface and then use another version of sort() which
takes 2 arguments...data to be sorted and your version of Comparator implementation.
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply. I am looking up Comparator interface to see what that is and how to use it. Ill post back once I check it out.
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked it up and I found a bubble sort. I am so confused. How do you compare only 1 part of an array and then print all the information stored in that array.

games[0].getGameName(); compare that to all the other getGameName() in the array and then print out to the screen the name, ident number, price, ect. That is where I am confused.

Everything I see about comparing strings returns a number. How do I take a number and then apply that to the order of the array.

I am sure you can see that I am lost. Please help explain this.
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have so far. It still doesn't work.


This is the code I used to sort the array. I was thinking that the first for loop is for the passes the sort makes and the second is to exchange the places in the array of the games. I am thinking you need to move all of the different aspects of the game object. I changed the names from string to int so I could compare them. The above code replaced line 40 of the OP.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need to write your logic of comparing two Gamedetails in compare method of Comparator.How you write it
is entirely your dicretion(Just make sure that you follow certain guidelines while implementing compare method which you can easily find in net).
Then just pass the array and your comparator instance to sort method. Sort would do all the stuff by itself.

Manish
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Graham wrote:Please help, the code that works for this plus an explanation would be awesome!

Alex,

I've already asked you once before HERE:

Please DON'T WRITE LONG LINES (←click).

I've broken yours up this time, but I won't do it again.
Oh, and use spaces for indentation, not TABs.

Thanks.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Graham wrote:This is what I have so far. It still doesn't work.

I'm now not sure what you want: Do you want to know how to sort a Java array, or do you want to understand how to write a sort routine in general?
If the latter, it seems an odd thing to be doing while you're writing a game.

As Manish already explained: the way to sort an array is with Arrays.sort(array, Comparator), so my advice would be to learn how to write a Comparator (see java.util.Comparator), rather than rolling your own sort routine - which is likely to be far inferior to Java's.

Winston
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to sort the array in the code I posted in the op. I keep being told to go http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html <--Here. I did and I read it but still have no idea what I am suppose to do. Can someone write out an example and comment the example. Telling me to write Arrays.sort doesn't help at all. Write it where. On a blank page? In word? I am sorry to be smart about it but come on. Then I am told to define how to sort it so that Array.sort knows what to do. HOW? I feel like I am asking for directions and being told to buy a map and drive my car. Thats it, no go down to this street, turn right here. So to all you guys who know how to do this I'll explain something to you. I don't know anything about java or programming. I need it explained to me step by step. Please I beg you. This may be an easy problem for you but I have been searching the internet and watching youtube for 3 DAYS NOW! A real answer besides figure it out would be nice.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Write a class that implements Comparator<YOUR_TYPE>.
2. Write a method in that class with signature int compare(YOUR_TYPE o1, YOUR_TYPE o2) (you have to do so or it won't even compile).
3. That method should return a negative value if o1 is smaller than o2, zero if o1 is equal to o2 (don't confuse this with equals method) or a positive value when o1 is larger than o2.
4. What smaller, equal or larger means is up to you. Write appropriate code.
5. Suppose you have an array of type YOUR_TYPE[] called array and an instance of your Comparator<YOUR_TYPE> called comparator. Just use Arrays.sort(array, comparator); and voilà! Your array is sorted!
 
Alex Graham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That is what I needed.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Graham wrote:I did and I read it but still have no idea what I am suppose to do.

OK, admittedly, the docs could do with a simple example. But first you have to understand that if you want to sort something, you have to know what order to sort them in. If the object already implements Comparable (java.lang.Comparable) as do classes like String, then you don't have to worry about it; but your GameDetails class doesn't, so you need a way of telling sort() how to determine if one GameDetails object gd1 is greater, less, or equal to, another gd2.

Can someone write out an example and comment the example.

Here goes:but I'm not going to spoon-feed you. NOW re-read the docs for Comparator, and see how that might work.

Telling me to write Arrays.sort doesn't help at all. Write it where. On a blank page?

Don't be absurd. Where do you think you need to write it? We expect you to ShowSomeEffort here.

This may be an easy problem for you but I have been searching the internet and watching youtube for 3 DAYS NOW! A real answer besides figure it out would be nice.

Then I suggest you look at the tutorials.

Winston
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember to read CAREFULLY what are rules your compare method should obey.
here
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:And remember to read CAREFULLY what are rules your compare method should obey.
here

Very true. In fact, that deserves a cow. Don't worry; that's GOOD.

Winston
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Very true. In fact, that deserves a cow. Don't worry; that's GOOD.

Wow! My first own cow. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!