how to sort an simple array logically (for both string & int) ?
without using Arrays.sort(arr) or any comparators.
how to sort an simple array logically (for both string & int) ?
without using Arrays.sort(arr) or any comparators.
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.
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()
?
Here is a nice selection including code examples for the various ways to sort arrays and/or collections: Rosetta Code
Write a quicksort or mergesort algorithm. Smells homework, too.
You can write bubble sort or quick sort or any sorting algorithm yourself