File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting descendingly, but im after ascendingly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting descendingly, but im after ascendingly" Watch "Sorting descendingly, but im after ascendingly" New topic
Author

Sorting descendingly, but im after ascendingly

Richard Shelly
Greenhorn

Joined: Oct 03, 2005
Posts: 24


Ok, heres the thing, i wrote this, throughoughly intending for it to sort my objects in ascending order. for whatever reason, i've somehow rigged it to sort in descending order. anyone able to tell me how to fix this? (and yes, im praying it wont involve massive code revamping)


<a href="http://www.somethingfm.net/" target="_blank" rel="nofollow">somthingFM</a> BOOYAH
Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
Your code can be sortened up quite a bit in two words: bubble sorting. Ever heard of it? It involves the use of nested for loops and an array. So, while you're counting the steps in order of your array , you are also keeping track of the numbers you're using (ascending or descending). Look up bubble sorting on google, it might clear some stuff up. I got two examples from class that might help ya.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18856
    
    8

How would you fix it? Let's disregard solutions where you rewrite the whole thing with an inferior sort method, since you already said you didn't want a major rewrite. Instead, find the places where you compare two Tickets, like this one:and reverse the comparison, thus:But don't alter anything where you're comparing the array indexes of two tickets.

I'm assuming the purpose of this exercise is so you can learn about sorting algorithms. For some reason computer science courses seem to think this is a useful skill. But if you just need an array sorted, I would suggest the Arrays.sort() method.
Richard Shelly
Greenhorn

Joined: Oct 03, 2005
Posts: 24
thanks all, but ive worked it out (thats twice now ive had a problem, then figured it out myself while awaiting some sort of forum hope, i feel like im wasting peoples time).

Also, i would use bubble sort, except for its horrendous inefficiency. also, can you use Arrays.sort() on a class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Arrays.sort()? Can you apply it to an object? Apart from the fact that all arrays are objects in their own right . . .?
Please look up the details here: click on Arrays, then METHOD, then find sort(Object[]).
You will see that you can sort an array of Objects, but they have to be Comparable, which means they have to implement the interface of that name.

You can download many of the classes of the standard API as a compressed file, which when uncompressed allows you to see the entire text of the API classes. But I cna't remember where I got it from. You can read the Arrays class and its sort(Object[] a) method.

You are not wasting people's time by asking quesitons, but we would appreciate it if you post how you sorted out your problems; everybody learns like that.

CR
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30894
    
158

Originally posted by Campbell Ritchie:
Arrays.sort()? Can you apply it to an object? Apart from the fact that all arrays are objects in their own right . . .?
Please look up the details here: click on Arrays, then METHOD, then find sort(Object[]).
You will see that you can sort an array of Objects, but they have to be Comparable, which means they have to implement the interface of that name.

You can also use the signature that takes a Comparator. This approach works well if you can't change the original object.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
For some reason computer science courses seem to think this is a useful skill.

Perhaps it is because being able to analyze algorithm efficiency is quite useful (and in a nerdy sort of way, a fun exercise at times)? Because computer science is more than "how to program"?

Just a couple thoughts anyway.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18856
    
    8

Originally posted by Chad Clites:
Perhaps it is because being able to analyze algorithm efficiency is quite useful (and in a nerdy sort of way, a fun exercise at times)? Because computer science is more than "how to program"?
Those things may be true. But in 25 years of professional business programming I have never encountered a situation where it mattered how data was sorted, nor have I ever had to write code to sort data. My opinion is that teaching sorting is the generals training the troops to fight the previous war.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Like was said, computer science goes well beyond a "insert programing language here" tools end-user. Who makes those tools and compilers? CS and CSE grads. A grad might work with a programming language team, and have to know how things actually work underneath an API, because they have to write it. They might be working in a enbedded area where there are no pre-existing library for a certain functionality. Knowing how to write efficient algorithms is very applicable.

Knowing how different API libraries work underneath makes for a better programmer even if they never have to actually write them.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18856
    
    8

Originally posted by Rusty Shackleford:
Knowing how different API libraries work underneath makes for a better programmer even if they never have to actually write them.
Do you have any actual evidence for that statement? Or are you just echoing received opinions? Personally I think it's the other way around. Better programmers can tell how things work without having to be taught. But I don't have any evidence for that either.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Just my own empirical evidence.

Learning how to use the ArrayList or Vector class, for example is a pretty trivial task. Rolling your own isn't too hard, but writing an efficient implementation is a pretty good challenge.

In the real world, most programmers will never need to write either, but the experience in analyzing code and making it efficient is invaluable to every programmer, whether they just use tools or write them. Besides, sometimes the classes in an API are not the most efficient and knowing when and how to do that is also a good skill. I have written linked list implementations for specific assignments, and thus are not reusable. But it slightly outperformed the LinkedList class in java.

A good personal example are programming contests. Most of the problems are strictly academic, but everytime I participate my programming skills increase.

My computer science courses give that and a lot more, hardware and OS design, the mathematics and physics of computing, and a lot more. It is like buying an engine kit, just about anyone can do that, but can they design and build their own engine, like an engineer? Same thing with just learning basic programming concepts and API's.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
A century ago the engineer, manufacturer and pilot of an aircraft were one in the same. Today I doubt any one person fully understands the science behind every single system in a modern aircraft. Technology evolved and those involved became increasingly specialized and diverse. While this may not be a perfect analogy I think it applies to all technology. Computer science is no exception and as technology continues to develop we are inevitably going to become more and more specialized. What concern does an enterprise developer have for the engineering behind the latest processors? Should they actually know how to design and build a processor themselves? Of course not. The jobs we all have to fulfill are becoming more and more diverse. Perhaps academia is just slow to adapt and offer new programs to meet evolving technology, still trying to treat us all as if we were the same when we no longer are. We can't all be pigeon holed into the same skill set anymore.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting descendingly, but im after ascendingly