aspose file tools*
The moose likes Java in General and the fly likes Sorting objects in a cast in alphabetical order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting objects in a cast in alphabetical order" Watch "Sorting objects in a cast in alphabetical order" New topic
Author

Sorting objects in a cast in alphabetical order

Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

I need to make a program that prompts the user to input show names, day of new episode and time then store them into an array. But because I'm not allowed to use 1.7, and I have to use version 1.4. I can't use generics, unfortunately.

So I declared the List like this


And I need to make either a bubble sort or selection sort method.

I wrote this method to sort by time, but now I need to sort the shows by alphabetical order.



But with the alphabetical order, I wrote it like this. I KNOW it's wrong, so any idea about how I should write it and output it? Because I'm completely thrown off!



Any help would be great! I need to hand this in today! Thanks in advance.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

If you've got code that's working to sort based on time, then you should be able to get it to sort in a different way with a single change. Only the comparison needs to change - to use compareTo on whatever field you want to sort on.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8014
    
  22

Salma Youssef wrote:I wrote this method to sort by time...

Salma,

Please DontWriteLongLines. It makes your thread very hard to read,
and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Thanks.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8014
    
  22

Salma Youssef wrote:So I declared the List like this

Well that's where some of your problems start. It should be:
static List<ShowInfo> show = new ArrayList<ShowInfo>();
then you can get rid of about a gazillion casts.

And also, it should be ShowInfo, not showInfo. Class names start with capital letters.

Winston
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Winston, you missed this statement from the original post:

Salma Youssef wrote:But because I'm not allowed to use 1.7, and I have to use version 1.4. I can't use generics, unfortunately.
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

Winston Gutkowski wrote:
Salma Youssef wrote:So I declared the List like this

Well that's where some of your problems start. It should be:
static List<ShowInfo> show = new ArrayList<ShowInfo>();
then you can get rid of about a gazillion casts.

And also, it should be ShowInfo, not showInfo. Class names start with capital letters.

Winston


I'm not allowed to use generics because I'm not allowed to use any version of Java besides 1.4.2. It's ancient, I know, and I can't use half the stuff I should be using. But unfortunately, my teacher's insistent on it and won't be wavered. I've managed to make it work by casting (against my own will). Now I've tried changing the sortTime method so that it works alphabetically, but it doesn't seem to be working.

Here it is now:


Could you tell me what I'm doing wrong?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You still have different loops to the version you had that you said worked (to sort by time). Why have you changed them?

Also - this isn't the problem but a simplification - there's no need to swap each field of showInfo separately. Just swap the whole object. You can do in three lines what you're currently doing in 10.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8014
    
  22

Paul Clapham wrote:Winston, you missed this statement from the original post:

Oops, my apologies. However:

@Salma: You can still save yourself a LOT of unnecessary casting though:I suggest you also look up the List.set() method, because it allows you to swap elements incredibly easily - In fact, you can do it in one line of code without creating a new element.

But your basic problem is that you've written a fabulous amount of code without testing it every step of the way.

One thing I see is that you appear to be mixing up i's and j's in that enormous inner loop of yours, but what you really need to do is to StopCoding (←click) and work out on paper (or with println() statements) what your code is actually doing.
You already know what you want it to do, but it plainly isn't doing it; so you need to work out why. And for that, you need to debug.

And next time, don't write more than about 10 lines of code without compiling it AND testing it.

PS: I already asked you once politely: Please DontWriteLongLines. If you want people to answer your questions, it pays to make it easy for them to read your threads. I broke your enormous lines up again, but I will NOT do it any more.

Winston
 
Consider Paul's rocket mass heater.
 
subject: Sorting objects in a cast in alphabetical order