views:

411

answers:

4

I want to sort a vector contains like [a,b,1,3,5,z] both ascending and descending on Java ME, i.e. without using function like Collections.sort()

A: 

If it's a vector you can have a look at this example:

http://www.java-examples.com/sort-java-vector-descending-order-using-comparator-example

Cristian Boariu
"With out using function like Collections.sort() ..."
Jesper
+5  A: 

Implement a sorting algorithm yourself then.

Michael Borgwardt
A: 

Copy the implementation of Collections.sort(), paste and modify it so much that you will be able to claim that you have "only been inspired" by it.

It's not cheating, it's learning from the chosen implementation.

Joel
Yea right ... -1
Stephen C
not so that you can cheat. but it is always a good exercise to go to the source code to find out how things are actually done.
Ron Tuffin
This is really a poor, totally unprofessional, answer.
Paul Wagland
If you are asked, for whatever reason, to re-invent the wheel, what would you do?Check how the wheel has been invented in the first place and do the same - good professionals do exactly that
Joel
a) This is obviously a homework answer, not a "real world" question, telling them to copy the code is not helping them to learn. b) In the real world you might use it as a source of inspiration, but do you really want to expose yourself and your company to the legal nightmares of copyright infringement?
Paul Wagland
I guess we'll never agree on that. Anyway nothing is more "real" for Michael than his homework. Chances are that he'll take my advise, and if so, he'll get a best practice how to solve a problem.
Joel
Gareth Davis
+1  A: 

Exchange sort in 3 sentences:

  • Find the smallest item in the vector, and exchange it with the first element in the vector.
  • Sort the rest of the vector, i.e. pretend your vector starts at the next element after the first one (or whichever one you just did).
  • If there's no more "rest of the vector" because you've just allocated the last position, you're done.
Carl Smotricz