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

  • SEARCH
  • Home
  • 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 8632955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:25:20+00:00 2026-06-12T09:25:20+00:00

Below the code I am using works just fine and outputs the names except

  • 0

Below the code I am using works just fine and outputs the names except that the sort method is not working. I expected “Collections.sort(nameFromText);” to sort the ArrayList in alphabetical order by first name.

What am I doing wrong?

public static void main(String[] args) throws IOException {
    // Create and populate text file
    Writer textFile = new FileWriter("names.txt");
    String[] nameArray = new String[] { "Tina Tully\n", "Bill Simpson\n",
            "Dana Smith\n", "Ralph Andrews\n", "Greg Smithers\n",
            "Lisa Krump\n", "Gill Bitters\n", "Barbara West\n",
            "Sandra McDonald\n", "Bart Willis\n", "Bucky Zimmerman\n",
            "Richard Vicks\n", "Velma Tarp\n", "Winslow Tunnell\n",
            "Andrew Letterman\n", "Betty Trump\n", "Waldo Smith\n",
            "Kyle Ronno\n", "Vivian West\n", "Wendy Tunnell\n" };
    generateText(textFile, nameArray);

    // Create object of previously created text file
    Scanner pullFile = new Scanner(new File("names.txt"));

    // Create 20 Person Objects and add to ArrayList data structure with
    // name variables assigned to values from text file
    ArrayList<Person> nameFromText = new ArrayList<Person>();
    fillArrayList(nameFromText, pullFile);

    // Sort ArrayList
    Collections.sort(nameFromText);

    // Print ArrayList
    printNamesFromObjects(nameFromText);
}

private static void printNamesFromObjects(ArrayList<Person> namesFromText) {
    for (int i = 0; i < 20; i++) {
        System.out.println(namesFromText.get(i).name);
    }
}

private static void fillArrayList(ArrayList<Person> nameFromText,
        Scanner pullFile) {
    while (pullFile.hasNext()) {
        Person obj = new Person(pullFile.nextLine());
        nameFromText.add(obj);
    }
}

private static void generateText(Writer textFile, String[] nameArray)
        throws IOException {
    for (int i = 0; i < 20; i++) {
        textFile.write(new String(nameArray[i]));
    }
    textFile.close();
}
  • 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-12T09:25:21+00:00Added an answer on June 12, 2026 at 9:25 am

    Collections.sort(List<T>) method expects the element of the list it is sorting to be comparable. Either the element type T should implement the Comparable interface, or you should use the overloaded sort() method, that takes a generic Comparator instance.

    In the code below, you are satisfying neither of the above conditions. Neither your Person class implements Comparable, nor you are passing any Comparator instance.

    ArrayList<Person> nameFromText = new ArrayList<Person>();
    fillArrayList(nameFromText, pullFile);
    // Sort ArrayList
    Collections.sort(nameFromText);  // How to sort?
    

    You should create a Comparator for your Person class to tell the sort() method how to sort it (may be on String stored in Person class)

    Here’s how you implement a generic comparator:

    public class PersonNameComparator implements Comparator<Person> {
        public int compare(Person p1, Person p2) {
            return p1.getName().compareTo(p2.getName());
        }
    }
    

    And then your Collections.sort() method invocation should be like: –

    Collections.sort(nameFromText, new PersonNameComparator());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It works just fine, for plain vanilla functions. The code below works just fine.
Why does the below C code using strcpy work just fine for me? I
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I'm using the below code to generate a horizontal bar. It works in older
I'm using the code below for dragging an image ( #StndSoln1 ). It works
I'm rendering PDF pages on iPhone using the code below. It works, but the
I am using Polymorphic Models . Simple Question: My code below works without using
I'm using the jQuery code below to launch an overlay div and it works
Im trying to generate a color gradient using ColdFusion. My current code below works
Not quite as racy as it sounds - I'm using the below code to

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.