2

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()

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
karthickbabu
  • 21
  • 1
  • 4

4 Answers4

6

Implement a sorting algorithm yourself then.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

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
  • 66,391
  • 18
  • 125
  • 167
0

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
  • 9,603
  • 14
  • 91
  • 162
-1

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 Shemtov
  • 3,008
  • 2
  • 22
  • 22
  • 1
    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 Dec 10 '09 at 09:30
  • 1
    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 Shemtov Dec 10 '09 at 09:53
  • 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 Dec 10 '09 at 10:19
  • 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 Shemtov Dec 10 '09 at 10:45
  • 1
    assuming it's not homework.. it's a perfectly valid thing to do. What else are you doing to do? waste an hour or two implementing and testing a sort algorithm from Wikipedia or something, just cut&paste Collections.sort() – Gareth Davis Dec 10 '09 at 10:48