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

5
  • You need to be more specific. Are those just strings? Otherwise what is 'a' etc? Commented Dec 10, 2009 at 8:49
  • Why would you not want to use Collections.sort()? If it's a homework question, you should give it the homework tag Commented Dec 10, 2009 at 8:56
  • Why don't you want to use Collections.sort(..)? Homework?? Commented Dec 10, 2009 at 8:57
  • 2
    If it is homework, Bubble Sort is the way to go ;) Commented Dec 10, 2009 at 8:58
  • For using in Mobile Tech.Its not support like Collections.sort().. Commented Dec 10, 2009 at 9:47

4 Answers 4

6

Implement a sorting algorithm yourself then.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

"With out using function like Collections.sort() ..."
-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.

5 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.