Hello. I am working on this project and it asks me to make two columns of words that are ascending in one column and descending in the other. This is the Project:
Call both your project and class AscendDescend. The first column should be in ascending order and the second indescending order. The output should appear as below (Be sure to include the headers):

Ascend

Agnes
Alfred
Alvin
Bernard
Bill
Ezra
Herman
Lee
Mary
Thomas

Descend

Thomas
Mary
Lee
Herman
Ezra
Bill
Bernard
Alvin
Alfred
Agnes

This is my code so far:

import java.util.*;
public class twoforone
{
  public static void main(String args[])
  {
    String theArray[] = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Ezra", "Herman"};
    for(int j = 0; j < theArray.length; j++)
    {
      Arrays.sort(theArray);
      System.out.println(theArray[j] + " ");
    }
    System.out.println(" ");
    {
      for(int m = 0; m < theArray.length; m++)
      {
        Arrays.sort(theArray);
      System.out.println(theArray[m] + " ");
      }
    }
  }
}

Please send me some feedback on this. I need help with the descending column. I dont know the code to descend. Please help.

Once you have the array sorted ascending then you get the descending order by simply starting with the last element and working all the way back to the first. Ie, if there are (n+1) entries entries, then the ascending sequence is indexes 0 - n, and the descending sequence is indexes n - 0. More generally: if you have an "i = 0 to n" loop, then the ascending indexes are just [i], and the descending are indexes are [n-i].

Ps: Looking at your code: you just need to sort the array once, there's no need to sort it on every pass of every loop.

You lost me. I am extremely new to this.

You can do this:

String[] theArray = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Ezra", "Herman"};
List<String> ascendList = Arrays.asList(theArray);
List<String> descendList = Arrays.asList(theArray);

Collections.sort(ascendList); //ASCENDED
Collections.sort(descendList, Collections.reverseOrder()); //DESCEND LIST

To show the list: (same for descendList)

for(String item : ascendList){
    System.out.println(item);
}

Hope this helps.

Look at this simple way.
Change you code with this:

import java.util.*;
public class twoforone
{
public static void main(String args[])
{
    String theArray[] = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Ezra", "Herman"};

    Arrays.sort(theArray);  //This sort your array
    System.out.println("Ascend");
    //undergoing the array in normal order
    for(int j = 0; j < theArray.length; j++)
    {
        System.out.println(theArray[j] + " ");
    }
    System.out.println(" ");
    System.out.println("Descend");
    //undergoing the array in revers order
    for(int m = theArray.length; m < -1 ; m--)
    {

        System.out.println(theArray[m] + " ");
    }

}

yes, you can do that but it takes up a lot of extra memory and CPU cycles as supposed to simply looping through the already existed and sorted array in reverse order.

wrong.
Sorting 2 times make your code to be slower, anyway you need to undergoig the array 2 times to display it content.

Just look carrefully at my code.
Your code is bad becouse you sort the array in for cicle... that is very wrong.
The sorting must do just one time, how i put in my code.

you're both wrong in that you both attempt to give the kid a complete working solution to his homework assignment.
That's against site policies and for very good reasons.

I'm not entirely sure if this would work, but you cuold try using the bucket/radix sort method. The reason I say not sure, is because when I did for a class project, we used number, not strings of characters. But if it does work, it's worth a try. All you have to do is redesign the method to perform in ascending order, then create another method, and rewrite it to display in descending order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.