• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting descendingly, but im after ascendingly

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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)
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic