Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:00:44+00:00 2026-06-04T07:00:44+00:00

Possible Duplicate: Sorted ArrayList not displaying? Code: String title1 = Original Order\n\n; String title2

  • 0

Possible Duplicate:
Sorted ArrayList not displaying?

Code:

  String title1 = "Original Order\n\n";
  String title2 = "Sorted Order\n\n";

  String collection = "";
  for (int i = 0; i < cdcollection.size(); i++)  {
  collection = collection + cdcollection.get(i);
  }
  mainTextArea.setText(title1 + collection);


  Collections.sort(cdcollection);

  String temp = "";
  for (int j = 0; j < cdcollection.size(); j++)  {
  temp += cdcollection.get(j);
  }
  mainTextArea.setText(title2 + temp);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:00:44+00:00Added an answer on June 4, 2026 at 7:00 am

    I took your code without the swing code and the list is sorted properly. See the code below

    public static void main(String[] args){
        ArrayList<String> cdcollection = new ArrayList();
        Collections.addAll(cdcollection, "renier\n", "mert\n", "rain\n",
                "mylen", "dreb\n");
    
        String title1 = "Original Order\n\n";
        String title2 = "Sorted Order\n\n";
    
        String collection = "";
        for (int i = 0; i < cdcollection.size(); i++) {
            collection = collection + cdcollection.get(i);
        }
        System.out.println(title1 + collection);
    
        Collections.sort(cdcollection);
        System.out.println(cdcollection);
    }
    

    The for loop you used here

     for (int j = 0; j < cdcollection.size(); j++)  {
          mainTextArea.setText(title2 + collection.get(j));
     }
    

    Dont add the title2 in the loop and calling setText in loop will over write the old text. Append all the string from the list in a temp string and in the last set in the in the mainTextArea. Something like

     String temp = "";
     for (int j = 0; j < cdcollection.size(); j++)  {
          temp += cdcollection.get(j));
     }
     mainTextArea.setText(title2 + temp);
    

    But this is definitely a duplicate as mentioned by Alexander. you could have edited the same question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Given a 2d array sorted in increasing order from left to right
Possible Duplicate: Sorting list of lists by a third list of specified non-sorted order
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing
Possible Duplicate: Elements order - for (... in ...) loop in javascript Assume you
Possible Duplicate: Python analog of natsort function (sort a list using a “natural order”
Possible Duplicates: Given a 2d array sorted in increasing order from left to right
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of
Possible Duplicate: Is the order guaranteed for the return of keySet() of a LinkedHashMap
Possible Duplicate: C# okay with comparing value types to null Consider the following code
Possible Duplicate: Python not sorting unicode properly. Strcoll doesn't help. I'm trying to sort

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.