Sort a given string in ascending order
Your algorithm is selection sort.

Selection sort has a time complexity of \$O(n^2)\$. For sorting algorithms, that's pretty slow.

Consider looking up a sorting algorithm such as quicksort, which, whilst it does have a worst case performance of \$O(n^2)\$, on average, it tends to take \$O(n log n)\$.

Or, as suggested in the comments, make use of the built-in libraries. Use Arrays.sort. Use of library code, especially when part of the language that you're programming in, can save significantly on the work you need to do.



Splitting up your code into several methods can be done.

Take this section, for instance.

        for (int j = i + 1; j &lt;= lastPos; j++)
            if (str[j].compareTo (str[minPos]) &lt; 0)
                minPos = j;


What it does is it looks for the smallest string in the array. So you could turn this into a function, with arguments for the array and the starting position (array.length will get you the end).

The swap,

        if (minPos != i)
        {
            s = str[i];
            str[i] = str[minPos];
            str[minPos] = s;
        }


Could also be a separate method.

If you extract those two sections of code to separate functions, the main sorting function will look like this:

public static void sort (String[] str)
{
    int lastPos = str.length - 1;
    for (int i = 0; i &lt; lastPos; i++)
    {
        swap(str, i, findSmallest(str, i));
    }
}