views:

133

answers:

7
+2  Q: 

java array sorting

how to sort an simple array logically (for both string & int) ?

without using Arrays.sort(arr) or any comparators.

+1  A: 

Use Arrays.sort(). See the API documentation of class java.util.Arrays.

Example:

import java.util.Arrays;

// ...

String[] arr = new String[] { "one", "two", "three" };
Arrays.sort(arr);

edit - Ok, you've edited your question and added "without using Arrays.sort()". Is this homework? Are you supposed to implement your own sorting algorithm? Then just do some research on sorting algorithms and implement one yourself.

Jesper
"without using arrays sort method"?
roe
Thanks roe! It didn't say "without using arrays sort method" when I first answered the question.
Jesper
That's why you get the +1 from me. Changing the requirements after receiving some answers is not fair and the penalty for this should *only* go to the questioner ;)
Andreas_D
I didn't downvote, so don't blame me.. :) I hate it that you can edit your question without it showing up in history. Answers ok, but questions..? :P
roe
A: 

To add a little bit more to their answers above - as you mentioned "sorting logically", you can implement your own Comparator and use Arrays.sort(array, comparator).

Is there a specific reason you want to avoid Arrays.sort()?

yclian
+2  A: 

Here is a nice selection including code examples for the various ways to sort arrays and/or collections: Rosetta Code

Andreas_D
A: 

I have covered sorting algorithms in Java which includes the bubble sort, quick sort and merge sort with comprehensive code and explanations. You may find the code here.

Bragboy
+1  A: 

Write a quicksort or mergesort algorithm. Smells homework, too.

Ahmet Alp Balkan
For anyone obviously not knowing much about sorting this is a bad advice, as both are quite complex and hard to get right.
Helper Method
A: 

Have a look at Insertion Sort.

Helper Method
... I really don't think, that *this* reference is useful for the questioner. Insertion Sort may be a simple sorting algorithm but I doubt the rohitroy is studying computer science. (-1)
Andreas_D
+1  A: 

You can write bubble sort or quick sort or any sorting algorithm yourself

Rambo