karthickbabu
karthickbabu

Reputation: 21

Sorting vector in java

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

Upvotes: 2

Views: 2858

Answers (4)

Carl Smotricz
Carl Smotricz

Reputation: 67760

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.

Upvotes: 1

Joel Shemtov
Joel Shemtov

Reputation: 3078

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.

Upvotes: -1

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

Implement a sorting algorithm yourself then.

Upvotes: 6

Cristian Boariu
Cristian Boariu

Reputation: 9621

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

Upvotes: 0

Related Questions