2

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

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

8
  • 8
    "without using arrays sort method." - whoa, I smell homework. Commented Jul 19, 2010 at 8:29
  • 1
    You're asking how to do sorting in general? Wikipedia has great articles on this: en.wikipedia.org/wiki/Sorting_algorithm see the bottom for a bunch of different algorithms. @Andreas_D my thoughts exactly. Commented Jul 19, 2010 at 8:30
  • 1
    (you can't sort without comparing...) Commented Jul 19, 2010 at 8:32
  • compare but without using any comparators Commented Jul 19, 2010 at 8:35
  • 1
    strange limitation - why do you not allow implementing a Comparator, it could be used in a custom, flexible sorting algorithm. Otherwise you need an individual sorting algorithms for each and every data type. Doesn't make much sense. Commented Jul 19, 2010 at 8:38

8 Answers 8

4

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

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

Comments

1

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.

4 Comments

"without using arrays sort method"?
Thanks roe! It didn't say "without using arrays sort method" when I first answered the question.
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 ;)
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
1

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

1 Comment

For anyone obviously not knowing much about sorting this is a bad advice, as both are quite complex and hard to get right.
1

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

Comments

1

I write my own sorting algorithms in my personal projects without looking at any formal sorting algorithms. It keeps my programming edges sharpend, plus it's interesting to see what you can come up with on your own, compared to the more formal sorting algorithms.

Comments

0

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

Comments

0

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.

Comments

-1

Have a look at Insertion Sort.

1 Comment

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

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.